Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -31,36 +31,87 @@ def answer_question(text, question):
|
|
| 31 |
except Exception as e:
|
| 32 |
return f"Error in question answering: {str(e)}"
|
| 33 |
|
| 34 |
-
# Gradio
|
| 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 |
# Launch the app
|
| 66 |
-
|
|
|
|
| 31 |
except Exception as e:
|
| 32 |
return f"Error in question answering: {str(e)}"
|
| 33 |
|
| 34 |
+
# Gradio Blocks for advanced UI
|
| 35 |
+
with gr.Blocks(
|
| 36 |
+
theme=gr.themes.Soft(primary_hue="teal", secondary_hue="pink"), # Use a vibrant theme
|
| 37 |
+
css=".gradio-container {background: linear-gradient(135deg, #f5f7fa, #c3cfe2);} "
|
| 38 |
+
".output-text {font-family: 'Arial', sans-serif; font-size: 16px; color: #333;} "
|
| 39 |
+
".input-text {font-family: 'Arial', sans-serif; font-size: 16px; color: #555;} "
|
| 40 |
+
"button {background: linear-gradient(135deg, #6a11cb, #2575fc); color: white; border: none; padding: 10px 20px; border-radius: 5px;} "
|
| 41 |
+
"button:hover {background: linear-gradient(135deg, #2575fc, #6a11cb);} "
|
| 42 |
+
) as demo:
|
| 43 |
+
# Title and description
|
| 44 |
+
gr.Markdown(
|
| 45 |
+
"""
|
| 46 |
+
# π **AI-Powered Study Assistant**
|
| 47 |
+
**Summarize text, generate flashcards, or answer questions using AI!**
|
| 48 |
+
"""
|
| 49 |
+
)
|
| 50 |
|
| 51 |
+
# Inputs
|
| 52 |
+
with gr.Row():
|
| 53 |
+
text_input = gr.Textbox(
|
| 54 |
+
lines=10,
|
| 55 |
+
label="π **Input Text**",
|
| 56 |
+
placeholder="Paste your text here...",
|
| 57 |
+
elem_classes="input-text"
|
| 58 |
+
)
|
| 59 |
+
question_input = gr.Textbox(
|
| 60 |
+
lines=2,
|
| 61 |
+
label="β **Question (for Answer Question task)**",
|
| 62 |
+
placeholder="Enter your question here...",
|
| 63 |
+
elem_classes="input-text"
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
# Task selection
|
| 67 |
+
task_radio = gr.Radio(
|
| 68 |
+
choices=["Summarize", "Generate Flashcards", "Answer Question"],
|
| 69 |
+
label="π― **Task**",
|
| 70 |
+
value="Summarize"
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
# Output
|
| 74 |
+
output_text = gr.Textbox(
|
| 75 |
+
label="π **Output**",
|
| 76 |
+
lines=10,
|
| 77 |
+
elem_classes="output-text"
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
# Submit button
|
| 81 |
+
submit_button = gr.Button("β¨ **Submit**")
|
| 82 |
+
|
| 83 |
+
# Examples
|
| 84 |
+
gr.Examples(
|
| 85 |
+
examples=[
|
| 86 |
+
["The French Revolution was a period of radical social and political upheaval in France that lasted from 1789 to 1799. It led to the rise of Napoleon Bonaparte and the eventual decline of the French monarchy.", "Summarize"],
|
| 87 |
+
["Photosynthesis is the process by which green plants use sunlight to synthesize foods with the help of chlorophyll. It converts carbon dioxide and water into glucose and oxygen.", "Generate Flashcards"],
|
| 88 |
+
["The Industrial Revolution began in the 18th century and marked a major turning point in history. Almost every aspect of daily life was influenced in some way.", "Answer Question", "When did the Industrial Revolution begin?"],
|
| 89 |
+
],
|
| 90 |
+
inputs=[text_input, task_radio, question_input],
|
| 91 |
+
outputs=output_text,
|
| 92 |
+
fn=study_assistant,
|
| 93 |
+
label="π **Examples**"
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
# Function to handle task selection
|
| 97 |
+
def study_assistant(text, task, question=None):
|
| 98 |
+
if task == "Summarize":
|
| 99 |
+
return summarize(text)
|
| 100 |
+
elif task == "Generate Flashcards":
|
| 101 |
+
return generate_flashcards(text)
|
| 102 |
+
elif task == "Answer Question":
|
| 103 |
+
if not question:
|
| 104 |
+
return "Please enter a question."
|
| 105 |
+
return answer_question(text, question)
|
| 106 |
+
else:
|
| 107 |
+
return "Invalid task selected."
|
| 108 |
+
|
| 109 |
+
# Link button to function
|
| 110 |
+
submit_button.click(
|
| 111 |
+
study_assistant,
|
| 112 |
+
inputs=[text_input, task_radio, question_input],
|
| 113 |
+
outputs=output_text
|
| 114 |
+
)
|
| 115 |
|
| 116 |
# Launch the app
|
| 117 |
+
demo.launch()
|