study-assistant / app.py
tariqm603's picture
Update app.py
ad58576 verified
import gradio as gr
import pandas as pd
import os
# Function to load the dataset
def load_data():
if os.path.exists('chapter1_english_9th.xlsx'):
dataset = pd.read_excel('chapter1_english_9th.xlsx', sheet_name=None)
print(f"Dataset loaded successfully. Sheets available: {dataset.keys()}") # Debugging line
else:
raise FileNotFoundError("Dataset file not found! Please upload the 'chapter1_english_9th.xlsx' file.")
return dataset
# Load the dataset
dataset = load_data()
# Function to retrieve content based on exact selections
def get_content(selected_class, selected_subject, selected_chapter, content_type):
print(f"Selected Class: {selected_class}, Subject: {selected_subject}, Chapter: {selected_chapter}, Content Type: {content_type}") # Debugging line
if selected_class == 'Class 9' and selected_subject == 'English' and selected_chapter == 'Chapter 1':
sheet_mapping = {
'Short Questions': 'Sheet1',
'Synonyms and Urdu Meaning': 'Sheet2',
'Grammar Identification': 'Sheet3'
}
elif selected_class == 'Class 10' and selected_subject == 'English' and selected_chapter == 'Chapter 2':
sheet_mapping = {
'Short Questions': 'Sheet4',
'Synonyms and Urdu Meaning': 'Sheet5',
'Grammar Identification': 'Sheet6'
}
else:
print("No matching data for the given selection.") # Debugging line
return None
sheet_name = sheet_mapping.get(content_type)
# Check if sheet name exists
if sheet_name in dataset:
print(f"Found data for {sheet_name}") # Debugging line
return dataset[sheet_name]
else:
print(f"Sheet '{sheet_name}' not found in dataset.") # Debugging line
return None
# Groq Model Processing (Placeholder function)
def groq_process_data(content):
return content.head() # Returning first 5 rows for illustration
# Gradio interface function for new result page
def result_page(content):
return content
# Gradio interface function
def gradio_interface(selected_class, selected_subject, selected_chapter, content_type):
content = get_content(selected_class, selected_subject, selected_chapter, content_type)
if content is not None and not content.empty:
processed_content = groq_process_data(content)
# Launching a new page with the result
gr.Interface(fn=result_page, inputs=gr.Dataframe(value=processed_content), outputs=gr.Dataframe()).launch()
return processed_content
else:
return "No data available currently."
# Define the Gradio interface with attractive layout and fresh output page
def run_app():
with gr.Blocks() as demo:
# Header section
gr.Markdown("# Educational Content Access App")
gr.Markdown("### Select options to view educational content.")
# Input section: Using card-style layout for a neat appearance
with gr.Row():
with gr.Column(scale=1):
selected_class = gr.Dropdown(label="Select Class", choices=["Class 9", "Class 10", "Class 11", "Class 12"], interactive=True)
selected_subject = gr.Dropdown(label="Select Subject", choices=["Computer", "English", "Physics", "Chemistry", "Math"], interactive=True)
selected_chapter = gr.Dropdown(label="Select Chapter", choices=[f"Chapter {i}" for i in range(1, 13)], interactive=True)
content_type = gr.Dropdown(label="Select Content Type", choices=["Short Questions", "Synonyms and Urdu Meaning", "Grammar Identification"], interactive=True)
# Submit Button to generate content
submit_button = gr.Button("Show Content", variant="primary")
# Connecting the button to the output, results will be shown on a new page
submit_button.click(gradio_interface, inputs=[selected_class, selected_subject, selected_chapter, content_type], outputs=[])
demo.launch(share=True) # Launch the app with the share parameter to open it on a new page
# Run the app
run_app()