|
import gradio as gr |
|
import pandas as pd |
|
|
|
|
|
url = "TExam_new.csv" |
|
df = pd.read_csv(url, encoding='utf-8-sig') |
|
|
|
|
|
def search_years(search_by_year, search_by_keywords, query): |
|
if search_by_year and not search_by_keywords: |
|
|
|
matches = df[df['YEAR'].str.startswith(query[:4])] |
|
if matches.empty: |
|
return "No results found for your query." |
|
return ", ".join(matches['YEAR'].tolist()) |
|
elif search_by_keywords and not search_by_year: |
|
|
|
keyword_list = [keyword.strip() for keyword in query.split(',')] |
|
matches = df[df['KEYWORDS'].apply(lambda x: any(keyword in x for keyword in keyword_list))] |
|
if matches.empty: |
|
return "No results found for your query." |
|
return ", ".join(matches['YEAR'].tolist()) |
|
else: |
|
return "Please select exactly one search mode." |
|
|
|
|
|
|
|
|
|
|
|
def get_image_html(year): |
|
match = df[df['YEAR'] == year] |
|
if not match.empty: |
|
|
|
image_filename = match.iloc[0]['Filename'] |
|
|
|
image_url = f'https://huggingface.co/spaces/MK-316/TCE/raw/main/TExams/{image_filename}' |
|
keywords = match.iloc[0]['KEYWORDS'] |
|
|
|
return f"<b>π· Keywords:</b> π {keywords}<br><img src='{image_url}' width='800'/>" |
|
else: |
|
return "No keywords found for this year.", "No image found for this year." |
|
|
|
|
|
|
|
|
|
|
|
with gr.Blocks() as app: |
|
gr.Markdown("# Teacher Certificate Exam Searching Engine") |
|
gr.Markdown("## βοΈ [1] Search Data") |
|
|
|
|
|
with gr.Row(): |
|
search_by_year = gr.Checkbox(label="Search by YEAR", value=False) |
|
search_by_keywords = gr.Checkbox(label="Search Years by Keywords", value=False) |
|
|
|
|
|
with gr.Row(): |
|
search_input = gr.Textbox(label="Search Query: e.g., 2024 (by YEAR) or tapping (by Keywords)", |
|
placeholder="Enter year or keywords separated by commas") |
|
search_button = gr.Button("Click to Search") |
|
search_output = gr.Text(label="Results (file names)") |
|
|
|
|
|
search_button.click(fn=search_years, inputs=[search_by_year, search_by_keywords, search_input], outputs=search_output) |
|
|
|
gr.Markdown("## βοΈ [2] Display a Specific Exam Question Requested") |
|
with gr.Row(): |
|
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'") |
|
submit_button = gr.Button("Show me the exam question") |
|
image_output = gr.HTML() |
|
|
|
|
|
submit_button.click(fn=get_image_html, inputs=year_input, outputs=image_output) |
|
|
|
|
|
app.launch(debug=True) |
|
|