File size: 3,741 Bytes
35bb374
 
 
 
6d9789e
35bb374
 
 
 
 
 
 
 
b8fea6f
 
35bb374
 
 
 
 
b8fea6f
 
35bb374
b8fea6f
bffee5c
b8fea6f
35bb374
 
 
bffee5c
22c7097
bffee5c
63e67b9
5976c88
 
 
35bb374
5976c88
35bb374
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8fea6f
 
35bb374
 
b8fea6f
 
 
 
 
 
35bb374
 
 
b8fea6f
 
35bb374
 
b8fea6f
 
 
 
 
 
35bb374
b8fea6f
35bb374
 
fc50377
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import gradio as gr
import pandas as pd

# Load the DataFrame
url = "TExam_new.csv"
df = pd.read_csv(url, encoding='utf-8-sig')

# Function to search years based on the selected mode
def search_years(search_by_year, search_by_keywords, query):
    if search_by_year and not search_by_keywords:
        # Search by matching the first four characters of the 'YEAR' column
        matches = df[df['YEAR'].str.startswith(query[:4])]
        if matches.empty:
            return [], "No results found for your query."
        return matches['YEAR'].tolist(), "Search completed successfully."
    elif search_by_keywords and not search_by_year:
        # Original keyword search logic
        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 matches['YEAR'].tolist(), "Search completed successfully."
    else:
        return [], "Please select exactly one search mode."

# Function to get image HTML
def get_image_html(year):
    match = df[df['YEAR'] == year]
    if not match.empty:
        # Assuming 'LINK' column has the partial path like '2006_1.PNG'
        image_filename = match.iloc[0]['Filename']
        # Construct the full URL by appending the filename to the base path
        image_url = f'https://huggingface.co/spaces/MK-316/TCE/raw/main/TExams/{image_filename}'
        keywords = match.iloc[0]['KEYWORDS']
        # Place keywords above the image
        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."

# Create Gradio Blocks interface
with gr.Blocks() as app:
    gr.Markdown("# Teacher Certificate Exam Searching Engine")
    gr.Markdown("## ❄️ [1] Search Data")
    
    # Row for checkboxes
    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)
    
    # Row for search query, button and output
    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.Dropdown(label="Results (file names)", choices=[], visible=False)  # Hidden dropdown for results
        status_output = gr.Textbox(label="Status", visible=False)  # Hidden status textbox
    
    # Define actions
    def update_dropdown(results, status_message):
        return gr.update(choices=results), status_message

    search_button.click(fn=search_years, inputs=[search_by_year, search_by_keywords, search_input], 
                        outputs=[search_output, status_output], 
                        postprocess=update_dropdown)
    
    gr.Markdown("## ❄️ [2] Display a Specific Exam Question Requested")
    with gr.Row():
        year_input = gr.Dropdown(label="Select a file name from the results", choices=[])
        submit_button = gr.Button("Show me the exam question")
    image_output = gr.HTML()
    
    # Update the year input dropdown based on the search results
    def update_year_input(results):
        return gr.update(choices=results)

    search_output.change(fn=update_year_input, inputs=search_output, outputs=year_input)

    # Define action for the submit button
    submit_button.click(fn=get_image_html, inputs=year_input, outputs=image_output)

# Launch the app with sharing options
app.launch(debug=True)