Update app.py
Browse files
app.py
CHANGED
@@ -6,33 +6,26 @@ url = "TExam_new.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(
|
10 |
-
if
|
11 |
-
# Search by matching the first four characters of the 'YEAR' column
|
12 |
matches = df[df['YEAR'].str.startswith(query[:4])]
|
13 |
-
|
14 |
-
return [], "No results found for your query."
|
15 |
-
return matches['YEAR'].tolist(), "Search completed successfully."
|
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 matches['YEAR'].tolist(), "Search completed successfully."
|
23 |
else:
|
24 |
return [], "Please select exactly one search mode."
|
25 |
|
|
|
|
|
|
|
|
|
26 |
# Function to get image HTML
|
27 |
def get_image_html(year):
|
28 |
match = df[df['YEAR'] == year]
|
29 |
if not match.empty:
|
30 |
-
# Assuming 'LINK' column has the partial path like '2006_1.PNG'
|
31 |
image_filename = match.iloc[0]['Filename']
|
32 |
-
# Construct the full URL by appending the filename to the base path
|
33 |
image_url = f'https://huggingface.co/spaces/MK-316/TCE/raw/main/TExams/{image_filename}'
|
34 |
keywords = match.iloc[0]['KEYWORDS']
|
35 |
-
# Place keywords above the image
|
36 |
return f"<b>π· Keywords:</b> π {keywords}<br><img src='{image_url}' width='800'/>"
|
37 |
else:
|
38 |
return "No keywords found for this year.", "No image found for this year."
|
@@ -42,40 +35,25 @@ with gr.Blocks() as app:
|
|
42 |
gr.Markdown("# Teacher Certificate Exam Searching Engine")
|
43 |
gr.Markdown("## βοΈ [1] Search Data")
|
44 |
|
45 |
-
#
|
46 |
-
|
47 |
-
search_by_year = gr.Checkbox(label="Search by YEAR", value=False)
|
48 |
-
search_by_keywords = gr.Checkbox(label="Search Years by Keywords", value=False)
|
49 |
|
50 |
-
# Row for search query
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
search_output = gr.Dropdown(label="Results (file names)", choices=[], visible=False) # Hidden dropdown for results
|
56 |
-
status_output = gr.Textbox(label="Status", visible=False) # Hidden status textbox
|
57 |
|
58 |
-
#
|
59 |
-
|
60 |
-
return gr.update(choices=results), status_message
|
61 |
-
|
62 |
-
search_button.click(fn=search_years, inputs=[search_by_year, search_by_keywords, search_input],
|
63 |
-
outputs=[search_output, status_output],
|
64 |
-
postprocess=update_dropdown)
|
65 |
|
66 |
gr.Markdown("## βοΈ [2] Display a Specific Exam Question Requested")
|
67 |
-
|
68 |
-
|
69 |
-
submit_button = gr.Button("Show me the exam question")
|
70 |
image_output = gr.HTML()
|
71 |
|
72 |
-
# Update
|
73 |
-
|
74 |
-
return gr.update(choices=results)
|
75 |
-
|
76 |
-
search_output.change(fn=update_year_input, inputs=search_output, outputs=year_input)
|
77 |
-
|
78 |
-
# Define action for the submit button
|
79 |
submit_button.click(fn=get_image_html, inputs=year_input, outputs=image_output)
|
80 |
|
81 |
# Launch the app with sharing options
|
|
|
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_mode, query):
|
10 |
+
if search_mode == "Search by YEAR":
|
|
|
11 |
matches = df[df['YEAR'].str.startswith(query[:4])]
|
12 |
+
elif search_mode == "Search Years by Keywords":
|
|
|
|
|
|
|
|
|
13 |
keyword_list = [keyword.strip() for keyword in query.split(',')]
|
14 |
matches = df[df['KEYWORDS'].apply(lambda x: any(keyword in x for keyword in keyword_list))]
|
|
|
|
|
|
|
15 |
else:
|
16 |
return [], "Please select exactly one search mode."
|
17 |
|
18 |
+
if matches.empty:
|
19 |
+
return [], "No results found for your query."
|
20 |
+
return matches['YEAR'].tolist(), "Search completed successfully."
|
21 |
+
|
22 |
# Function to get image HTML
|
23 |
def get_image_html(year):
|
24 |
match = df[df['YEAR'] == year]
|
25 |
if not match.empty:
|
|
|
26 |
image_filename = match.iloc[0]['Filename']
|
|
|
27 |
image_url = f'https://huggingface.co/spaces/MK-316/TCE/raw/main/TExams/{image_filename}'
|
28 |
keywords = match.iloc[0]['KEYWORDS']
|
|
|
29 |
return f"<b>π· Keywords:</b> π {keywords}<br><img src='{image_url}' width='800'/>"
|
30 |
else:
|
31 |
return "No keywords found for this year.", "No image found for this year."
|
|
|
35 |
gr.Markdown("# Teacher Certificate Exam Searching Engine")
|
36 |
gr.Markdown("## βοΈ [1] Search Data")
|
37 |
|
38 |
+
# Radio buttons to select search mode
|
39 |
+
search_mode = gr.Radio(choices=["Search by YEAR", "Search Years by Keywords"], label="Search Mode")
|
|
|
|
|
40 |
|
41 |
+
# Row for search query and button
|
42 |
+
search_input = gr.Textbox(label="Search Query: e.g., 2024 (by YEAR) or tapping (by Keywords)", placeholder="Enter year or keywords")
|
43 |
+
search_button = gr.Button("Click to Search")
|
44 |
+
search_output = gr.Dropdown(label="Results (file names)", choices=[], visible=False)
|
45 |
+
status_output = gr.Textbox(label="Status", visible=False)
|
|
|
|
|
46 |
|
47 |
+
# Connect actions
|
48 |
+
search_button.click(fn=search_years, inputs=[search_mode, search_input], outputs=[search_output, status_output])
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
gr.Markdown("## βοΈ [2] Display a Specific Exam Question Requested")
|
51 |
+
year_input = gr.Dropdown(label="Select a file name from the results", choices=[])
|
52 |
+
submit_button = gr.Button("Show me the exam question")
|
|
|
53 |
image_output = gr.HTML()
|
54 |
|
55 |
+
# Update dropdown and image display
|
56 |
+
search_output.change(fn=lambda results: gr.update(choices=results), inputs=search_output, outputs=year_input)
|
|
|
|
|
|
|
|
|
|
|
57 |
submit_button.click(fn=get_image_html, inputs=year_input, outputs=image_output)
|
58 |
|
59 |
# Launch the app with sharing options
|