Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
|
4 |
+
# Load the DataFrame
|
5 |
+
url = "https://raw.githubusercontent.com/MK316/TExams/main/TExams.csv"
|
6 |
+
df = pd.read_csv(url, encoding='utf-8-sig')
|
7 |
+
|
8 |
+
# Function to search years based on the selected mode
|
9 |
+
def search_years(search_by_year, search_by_keywords, query):
|
10 |
+
if search_by_year and not search_by_keywords:
|
11 |
+
# Search by matching the first four characters of the 'YEAR' column
|
12 |
+
matches = df[df['YEAR'].str.startswith(query[:4])]
|
13 |
+
if matches.empty:
|
14 |
+
return "No results found for your query."
|
15 |
+
return ", ".join(matches['YEAR'].tolist()) # Return a string of matched years
|
16 |
+
elif search_by_keywords and not search_by_year:
|
17 |
+
# Original keyword search logic
|
18 |
+
keyword_list = [keyword.strip() for keyword in query.split(',')]
|
19 |
+
matches = df[df['KEYWORDS'].apply(lambda x: any(keyword in x for keyword in keyword_list))]
|
20 |
+
if matches.empty:
|
21 |
+
return "No results found for your query."
|
22 |
+
return ", ".join(matches['YEAR'].tolist()) # Return a string of matched years
|
23 |
+
else:
|
24 |
+
return "Please select exactly one search mode."
|
25 |
+
|
26 |
+
# Function to return an HTML image tag with the image URL
|
27 |
+
def get_image_html(year):
|
28 |
+
match = df[df['YEAR'] == year]
|
29 |
+
if not match.empty:
|
30 |
+
image_url = match.iloc[0]['LINK']
|
31 |
+
return f"<img src='{image_url}' width='800'/>"
|
32 |
+
else:
|
33 |
+
return "No image found for this year."
|
34 |
+
|
35 |
+
# Create Gradio Blocks interface
|
36 |
+
with gr.Blocks() as app:
|
37 |
+
gr.Markdown("# Teacher Certificate Exam Searching Engine")
|
38 |
+
gr.Markdown("## ❄️ [1] Search Data")
|
39 |
+
|
40 |
+
# Row for checkboxes
|
41 |
+
with gr.Row():
|
42 |
+
search_by_year = gr.Checkbox(label="Search by YEAR", value=False)
|
43 |
+
search_by_keywords = gr.Checkbox(label="Search Years by Keywords", value=False)
|
44 |
+
|
45 |
+
# Row for search query, button and output
|
46 |
+
with gr.Row():
|
47 |
+
search_input = gr.Textbox(label="Search Query: e.g., 2024 (by YEAR) or tapping (by Keywords)",
|
48 |
+
placeholder="Enter year or keywords separated by commas")
|
49 |
+
search_button = gr.Button("Click to Search")
|
50 |
+
search_output = gr.Text(label="Results (file names)")
|
51 |
+
|
52 |
+
# Define actions
|
53 |
+
search_button.click(fn=search_years, inputs=[search_by_year, search_by_keywords, search_input], outputs=search_output)
|
54 |
+
|
55 |
+
gr.Markdown("## ❄️ [2] Display a Specific Exam Question Requested")
|
56 |
+
with gr.Row():
|
57 |
+
year_input = gr.Textbox(label="Type a file name among the result items: e.g., Year_item_part(a,b)", placeholder="Enter Year like '2024_01'")
|
58 |
+
submit_button = gr.Button("Submit") # Added submit button
|
59 |
+
image_output = gr.HTML()
|
60 |
+
|
61 |
+
# Define action for the submit button
|
62 |
+
submit_button.click(fn=get_image_html, inputs=year_input, outputs=image_output) # Use the button to trigger the display function
|
63 |
+
|
64 |
+
# Launch the app with sharing options
|
65 |
+
app.launch(share=True, debug=True)
|