CodeOpt / app.py
mgbam's picture
Update app.py
473cdbb verified
raw
history blame
2.54 kB
import gradio as gr
from huggingface_hub import InferenceClient
# Initialize Hugging Face Inference Client
client = InferenceClient()
# Function to use a model for generating content dynamically
def generate_content(selected_path):
"""
Generates dynamic content based on the selected learning path using an NLP model.
"""
prompt = f"Create a short lesson or problem for the topic: {selected_path}."
response = client.text_generation(
model="gpt2", # Replace with a more suitable Hugging Face model for educational tasks
inputs=prompt,
max_new_tokens=150,
temperature=0.7
)
return response["generated_text"]
# Function to evaluate user input (for example, math problem answers)
def evaluate_answer(user_input, selected_path):
"""
Evaluates user input using the model and provides feedback.
"""
prompt = f"Evaluate the following response for {selected_path}:\n\nUser Answer: {user_input}\nProvide feedback."
response = client.text_generation(
model="gpt2", # Replace with a more appropriate evaluation model
inputs=prompt,
max_new_tokens=100,
temperature=0.5
)
return response["generated_text"]
# Gradio interface
with gr.Blocks() as app:
gr.Markdown("## AI-Powered Learning Platform")
gr.Markdown("Choose a topic to start learning:")
# Progress bar at the top (HTML-based)
gr.HTML('<div style="width: 100%; background-color: lightgray; height: 5px; border-radius: 5px;">'
'<div style="width: 25%; background-color: green; height: 100%;"></div></div>')
# Options for learning paths
selected_topic = gr.Radio(
choices=["Math", "Science & Engineering", "Computer Science & Programming", "Data Science & Data Analysis"],
label="Select a Learning Path",
value="Math"
)
# Buttons for user interaction
with gr.Row():
generate_button = gr.Button("Generate Content")
submit_answer_button = gr.Button("Submit Answer")
# Outputs
content_output = gr.Textbox(lines=8, interactive=False, label="Generated Content")
answer_input = gr.Textbox(lines=2, label="Your Answer")
feedback_output = gr.Textbox(interactive=False, label="Feedback")
# Link interactions to actions
generate_button.click(fn=generate_content, inputs=selected_topic, outputs=content_output)
submit_answer_button.click(fn=evaluate_answer, inputs=[answer_input, selected_topic], outputs=feedback_output)
# Launch the app
app.launch()