Update app.py
Browse files
app.py
CHANGED
|
@@ -8,7 +8,7 @@ client = InferenceClient()
|
|
| 8 |
# Function to generate dynamic lessons, examples, or code
|
| 9 |
def generate_content(selected_topic, subtopic, complexity, input_text, examples_count, output_type):
|
| 10 |
"""
|
| 11 |
-
Generate content dynamically based on user input.
|
| 12 |
|
| 13 |
Args:
|
| 14 |
selected_topic (str): Topic selected by the user.
|
|
@@ -16,10 +16,10 @@ def generate_content(selected_topic, subtopic, complexity, input_text, examples_
|
|
| 16 |
complexity (str): User expertise level.
|
| 17 |
input_text (str): Additional input context.
|
| 18 |
examples_count (int): Number of examples to generate.
|
| 19 |
-
output_type (str): Desired output format.
|
| 20 |
|
| 21 |
Returns:
|
| 22 |
-
|
| 23 |
"""
|
| 24 |
# Build the prompt dynamically
|
| 25 |
prompt = (
|
|
@@ -43,7 +43,9 @@ def generate_content(selected_topic, subtopic, complexity, input_text, examples_
|
|
| 43 |
|
| 44 |
# Adjust the output based on the selected type
|
| 45 |
if output_type == "LaTeX":
|
| 46 |
-
|
|
|
|
|
|
|
| 47 |
elif output_type == "Downloadable":
|
| 48 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".txt")
|
| 49 |
with open(temp_file.name, "w") as file:
|
|
@@ -56,11 +58,11 @@ def generate_content(selected_topic, subtopic, complexity, input_text, examples_
|
|
| 56 |
|
| 57 |
# Create the Gradio interface
|
| 58 |
with gr.Blocks() as app:
|
| 59 |
-
# App Title
|
| 60 |
-
gr.Markdown("## π Advanced STEM and Code Generator with
|
| 61 |
|
| 62 |
with gr.Row():
|
| 63 |
-
# Input
|
| 64 |
with gr.Column():
|
| 65 |
selected_topic = gr.Radio(
|
| 66 |
choices=["Math", "STEM", "Code Generation"],
|
|
@@ -95,24 +97,31 @@ with gr.Blocks() as app:
|
|
| 95 |
)
|
| 96 |
generate_button = gr.Button("Generate Content")
|
| 97 |
|
| 98 |
-
# Output
|
| 99 |
with gr.Column():
|
| 100 |
-
gr.Markdown("### π Output")
|
| 101 |
-
|
| 102 |
-
label="Generated Output",
|
| 103 |
-
lines=15,
|
| 104 |
-
interactive=False
|
| 105 |
-
)
|
| 106 |
download_button = gr.File(label="Download File (if applicable)")
|
| 107 |
|
| 108 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
generate_button.click(
|
| 110 |
fn=generate_content,
|
| 111 |
inputs=[selected_topic, subtopic, complexity, input_text, examples_count, output_type],
|
| 112 |
-
outputs=[
|
|
|
|
|
|
|
| 113 |
)
|
| 114 |
|
| 115 |
-
# Feedback
|
| 116 |
feedback_label = gr.Label(value="Was this content helpful?")
|
| 117 |
feedback_rating = gr.Radio(
|
| 118 |
choices=["Yes", "No"],
|
|
|
|
| 8 |
# Function to generate dynamic lessons, examples, or code
|
| 9 |
def generate_content(selected_topic, subtopic, complexity, input_text, examples_count, output_type):
|
| 10 |
"""
|
| 11 |
+
Generate content dynamically based on user input, with support for LaTeX rendering and file downloads.
|
| 12 |
|
| 13 |
Args:
|
| 14 |
selected_topic (str): Topic selected by the user.
|
|
|
|
| 16 |
complexity (str): User expertise level.
|
| 17 |
input_text (str): Additional input context.
|
| 18 |
examples_count (int): Number of examples to generate.
|
| 19 |
+
output_type (str): Desired output format (Plain Text, LaTeX, or Downloadable).
|
| 20 |
|
| 21 |
Returns:
|
| 22 |
+
dict or str: Generated content in the desired format.
|
| 23 |
"""
|
| 24 |
# Build the prompt dynamically
|
| 25 |
prompt = (
|
|
|
|
| 43 |
|
| 44 |
# Adjust the output based on the selected type
|
| 45 |
if output_type == "LaTeX":
|
| 46 |
+
# Wrap content in LaTeX delimiters for rendering
|
| 47 |
+
latex_content = f"$$\n{content}\n$$"
|
| 48 |
+
return {"content": latex_content, "latex": True}
|
| 49 |
elif output_type == "Downloadable":
|
| 50 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".txt")
|
| 51 |
with open(temp_file.name, "w") as file:
|
|
|
|
| 58 |
|
| 59 |
# Create the Gradio interface
|
| 60 |
with gr.Blocks() as app:
|
| 61 |
+
# App Title and Instructions
|
| 62 |
+
gr.Markdown("## π Advanced STEM and Code Generator with LaTeX Rendering and Downloads")
|
| 63 |
|
| 64 |
with gr.Row():
|
| 65 |
+
# Input Section
|
| 66 |
with gr.Column():
|
| 67 |
selected_topic = gr.Radio(
|
| 68 |
choices=["Math", "STEM", "Code Generation"],
|
|
|
|
| 97 |
)
|
| 98 |
generate_button = gr.Button("Generate Content")
|
| 99 |
|
| 100 |
+
# Output Section
|
| 101 |
with gr.Column():
|
| 102 |
+
gr.Markdown("### π Generated Output (Supports LaTeX)")
|
| 103 |
+
output_text = gr.Markdown(label="Generated Content")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
download_button = gr.File(label="Download File (if applicable)")
|
| 105 |
|
| 106 |
+
# Connect the generate function to the button
|
| 107 |
+
def update_output(result):
|
| 108 |
+
if isinstance(result, dict):
|
| 109 |
+
if "latex" in result:
|
| 110 |
+
return result["content"], None
|
| 111 |
+
elif "file" in result:
|
| 112 |
+
return "File ready for download.", result["file"]
|
| 113 |
+
else:
|
| 114 |
+
return result, None
|
| 115 |
+
|
| 116 |
generate_button.click(
|
| 117 |
fn=generate_content,
|
| 118 |
inputs=[selected_topic, subtopic, complexity, input_text, examples_count, output_type],
|
| 119 |
+
outputs=[output_text, download_button],
|
| 120 |
+
preprocess=False,
|
| 121 |
+
postprocess=update_output
|
| 122 |
)
|
| 123 |
|
| 124 |
+
# Feedback Section
|
| 125 |
feedback_label = gr.Label(value="Was this content helpful?")
|
| 126 |
feedback_rating = gr.Radio(
|
| 127 |
choices=["Yes", "No"],
|