Update app.py
Browse files
app.py
CHANGED
|
@@ -11,22 +11,19 @@ def search_years(search_by_year, search_by_keywords, query):
|
|
| 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
|
| 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
|
| 23 |
else:
|
| 24 |
-
return "Please select exactly one search mode."
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
# Modified function to return keywords above the HTML image tag
|
| 28 |
-
# ...
|
| 29 |
|
|
|
|
| 30 |
def get_image_html(year):
|
| 31 |
match = df[df['YEAR'] == year]
|
| 32 |
if not match.empty:
|
|
@@ -40,9 +37,6 @@ def get_image_html(year):
|
|
| 40 |
else:
|
| 41 |
return "No keywords found for this year.", "No image found for this year."
|
| 42 |
|
| 43 |
-
# ...
|
| 44 |
-
|
| 45 |
-
|
| 46 |
# Create Gradio Blocks interface
|
| 47 |
with gr.Blocks() as app:
|
| 48 |
gr.Markdown("# Teacher Certificate Exam Searching Engine")
|
|
@@ -58,19 +52,31 @@ with gr.Blocks() as app:
|
|
| 58 |
search_input = gr.Textbox(label="Search Query: e.g., 2024 (by YEAR) or tapping (by Keywords)",
|
| 59 |
placeholder="Enter year or keywords separated by commas")
|
| 60 |
search_button = gr.Button("Click to Search")
|
| 61 |
-
search_output = gr.
|
|
|
|
| 62 |
|
| 63 |
# Define actions
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
gr.Markdown("## ❄️ [2] Display a Specific Exam Question Requested")
|
| 67 |
with gr.Row():
|
| 68 |
-
year_input = gr.
|
| 69 |
-
submit_button = gr.Button("Show me the exam question")
|
| 70 |
image_output = gr.HTML()
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
# Define action for the submit button
|
| 73 |
-
submit_button.click(fn=get_image_html, inputs=year_input, outputs=image_output)
|
| 74 |
|
| 75 |
# Launch the app with sharing options
|
| 76 |
app.launch(debug=True)
|
|
|
|
| 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 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:
|
|
|
|
| 37 |
else:
|
| 38 |
return "No keywords found for this year.", "No image found for this year."
|
| 39 |
|
|
|
|
|
|
|
|
|
|
| 40 |
# Create Gradio Blocks interface
|
| 41 |
with gr.Blocks() as app:
|
| 42 |
gr.Markdown("# Teacher Certificate Exam Searching Engine")
|
|
|
|
| 52 |
search_input = gr.Textbox(label="Search Query: e.g., 2024 (by YEAR) or tapping (by Keywords)",
|
| 53 |
placeholder="Enter year or keywords separated by commas")
|
| 54 |
search_button = gr.Button("Click to Search")
|
| 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 |
# Define actions
|
| 59 |
+
def update_dropdown(results, status_message):
|
| 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 |
with gr.Row():
|
| 68 |
+
year_input = gr.Dropdown(label="Select a file name from the results", choices=[])
|
| 69 |
+
submit_button = gr.Button("Show me the exam question")
|
| 70 |
image_output = gr.HTML()
|
| 71 |
|
| 72 |
+
# Update the year input dropdown based on the search results
|
| 73 |
+
def update_year_input(results):
|
| 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
|
| 82 |
app.launch(debug=True)
|