Update app.py
Browse files
app.py
CHANGED
|
@@ -5,22 +5,25 @@ from huggingface_hub import InferenceClient
|
|
| 5 |
client = InferenceClient()
|
| 6 |
|
| 7 |
# Function to stream content for Math, STEM, and Code Generation
|
| 8 |
-
def generate_stream(selected_topic, input_text):
|
| 9 |
"""
|
| 10 |
Generates dynamic lessons, solutions, or code snippets based on the selected topic.
|
| 11 |
|
| 12 |
Args:
|
| 13 |
selected_topic (str): The selected subject (e.g., Math, STEM, or Code Generation).
|
|
|
|
| 14 |
input_text (str): Additional input for contextual content generation.
|
|
|
|
| 15 |
|
| 16 |
Yields:
|
| 17 |
str: Incremental output content.
|
| 18 |
"""
|
| 19 |
# Create a topic-specific prompt
|
| 20 |
prompt = (
|
| 21 |
-
f"Generate
|
|
|
|
| 22 |
if input_text.strip() else
|
| 23 |
-
f"Generate
|
| 24 |
)
|
| 25 |
messages = [{"role": "user", "content": prompt}]
|
| 26 |
|
|
@@ -46,10 +49,10 @@ def generate_stream(selected_topic, input_text):
|
|
| 46 |
# Create the Gradio interface
|
| 47 |
with gr.Blocks() as app:
|
| 48 |
# App Title and Instructions
|
| 49 |
-
gr.Markdown("## π STEM Learning and Code Generator")
|
| 50 |
gr.Markdown(
|
| 51 |
-
"
|
| 52 |
-
"or Computer Science. Select a topic and
|
| 53 |
)
|
| 54 |
|
| 55 |
with gr.Row():
|
|
@@ -60,28 +63,53 @@ with gr.Blocks() as app:
|
|
| 60 |
label="Select a Topic",
|
| 61 |
value="Math" # Default selection
|
| 62 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
input_text = gr.Textbox(
|
| 64 |
lines=2,
|
| 65 |
-
label="
|
| 66 |
placeholder="Provide additional context (e.g., 'Explain calculus basics' or 'Generate Python code for sorting')."
|
| 67 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
generate_button = gr.Button("Generate Content")
|
| 69 |
|
| 70 |
# Output Section
|
| 71 |
with gr.Column():
|
| 72 |
gr.Markdown("### Generated Content")
|
| 73 |
-
output_stream = gr.
|
|
|
|
| 74 |
label="Output",
|
| 75 |
-
placeholder="Generated content will appear here...",
|
| 76 |
interactive=False
|
| 77 |
)
|
|
|
|
| 78 |
|
| 79 |
# Link the generate button to the streaming function
|
| 80 |
generate_button.click(
|
| 81 |
fn=generate_stream,
|
| 82 |
-
inputs=[selected_topic, input_text],
|
| 83 |
outputs=output_stream
|
| 84 |
)
|
| 85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
# Launch the Gradio app
|
| 87 |
app.launch()
|
|
|
|
| 5 |
client = InferenceClient()
|
| 6 |
|
| 7 |
# Function to stream content for Math, STEM, and Code Generation
|
| 8 |
+
def generate_stream(selected_topic, subtopic, input_text, examples_count):
|
| 9 |
"""
|
| 10 |
Generates dynamic lessons, solutions, or code snippets based on the selected topic.
|
| 11 |
|
| 12 |
Args:
|
| 13 |
selected_topic (str): The selected subject (e.g., Math, STEM, or Code Generation).
|
| 14 |
+
subtopic (str): Specific subtopic or category for more focused output.
|
| 15 |
input_text (str): Additional input for contextual content generation.
|
| 16 |
+
examples_count (int): Number of examples to generate.
|
| 17 |
|
| 18 |
Yields:
|
| 19 |
str: Incremental output content.
|
| 20 |
"""
|
| 21 |
# Create a topic-specific prompt
|
| 22 |
prompt = (
|
| 23 |
+
f"Generate {examples_count} detailed {selected_topic.lower()} examples, lessons, or problems "
|
| 24 |
+
f"focused on {subtopic}. Input context: {input_text}"
|
| 25 |
if input_text.strip() else
|
| 26 |
+
f"Generate {examples_count} beginner-level {selected_topic.lower()} lessons or examples on {subtopic}."
|
| 27 |
)
|
| 28 |
messages = [{"role": "user", "content": prompt}]
|
| 29 |
|
|
|
|
| 49 |
# Create the Gradio interface
|
| 50 |
with gr.Blocks() as app:
|
| 51 |
# App Title and Instructions
|
| 52 |
+
gr.Markdown("## π Enhanced STEM Learning and Code Generator")
|
| 53 |
gr.Markdown(
|
| 54 |
+
"Generate tailored lessons, problem-solving examples, or code snippets for Math, STEM, "
|
| 55 |
+
"or Computer Science. Select a topic, subtopic, and customize your experience!"
|
| 56 |
)
|
| 57 |
|
| 58 |
with gr.Row():
|
|
|
|
| 63 |
label="Select a Topic",
|
| 64 |
value="Math" # Default selection
|
| 65 |
)
|
| 66 |
+
subtopic = gr.Textbox(
|
| 67 |
+
lines=1,
|
| 68 |
+
label="Subtopic",
|
| 69 |
+
placeholder="Specify a subtopic (e.g., Algebra, Physics, Data Structures)."
|
| 70 |
+
)
|
| 71 |
input_text = gr.Textbox(
|
| 72 |
lines=2,
|
| 73 |
+
label="Context or Additional Input",
|
| 74 |
placeholder="Provide additional context (e.g., 'Explain calculus basics' or 'Generate Python code for sorting')."
|
| 75 |
)
|
| 76 |
+
examples_count = gr.Slider(
|
| 77 |
+
minimum=1,
|
| 78 |
+
maximum=5,
|
| 79 |
+
value=1,
|
| 80 |
+
step=1,
|
| 81 |
+
label="Number of Examples"
|
| 82 |
+
)
|
| 83 |
generate_button = gr.Button("Generate Content")
|
| 84 |
|
| 85 |
# Output Section
|
| 86 |
with gr.Column():
|
| 87 |
gr.Markdown("### Generated Content")
|
| 88 |
+
output_stream = gr.Textbox(
|
| 89 |
+
lines=20,
|
| 90 |
label="Output",
|
|
|
|
| 91 |
interactive=False
|
| 92 |
)
|
| 93 |
+
export_button = gr.Button("Export Code (if applicable)")
|
| 94 |
|
| 95 |
# Link the generate button to the streaming function
|
| 96 |
generate_button.click(
|
| 97 |
fn=generate_stream,
|
| 98 |
+
inputs=[selected_topic, subtopic, input_text, examples_count],
|
| 99 |
outputs=output_stream
|
| 100 |
)
|
| 101 |
|
| 102 |
+
# Export functionality for code snippets
|
| 103 |
+
def export_code(content):
|
| 104 |
+
with open("generated_code.py", "w") as file:
|
| 105 |
+
file.write(content)
|
| 106 |
+
return "Code exported successfully to generated_code.py!"
|
| 107 |
+
|
| 108 |
+
export_button.click(
|
| 109 |
+
fn=export_code,
|
| 110 |
+
inputs=[output_stream],
|
| 111 |
+
outputs=[output_stream]
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
# Launch the Gradio app
|
| 115 |
app.launch()
|