Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Load the Hugging Face model for text tasks
|
| 5 |
+
text_generator = pipeline("text2text-generation", model="google/flan-t5-small")
|
| 6 |
+
|
| 7 |
+
# --- Helper functions ---
|
| 8 |
+
def summarize_text(text):
|
| 9 |
+
prompt = f"Summarize in 3 sentences: {text}"
|
| 10 |
+
result = text_generator(prompt, max_length=100, do_sample=False)
|
| 11 |
+
return result[0]['generated_text']
|
| 12 |
+
|
| 13 |
+
def answer_question(topic, question):
|
| 14 |
+
prompt = f"Topic: {topic}\nAnswer this question: {question}"
|
| 15 |
+
result = text_generator(prompt, max_length=150, do_sample=False)
|
| 16 |
+
return result[0]['generated_text']
|
| 17 |
+
|
| 18 |
+
def generate_story(theme):
|
| 19 |
+
prompt = f"Write a 200-word story about: {theme}"
|
| 20 |
+
result = text_generator(prompt, max_length=300, do_sample=True)
|
| 21 |
+
return result[0]['generated_text']
|
| 22 |
+
|
| 23 |
+
# --- Gradio UI ---
|
| 24 |
+
with gr.Blocks() as demo:
|
| 25 |
+
gr.Markdown("# Mini AI Text Assistant")
|
| 26 |
+
|
| 27 |
+
task = gr.Radio(["Summarizer", "Q&A Bot", "Story Generator"], label="Choose a task")
|
| 28 |
+
|
| 29 |
+
input_text = gr.Textbox(label="Enter text or topic/theme", placeholder="Type here...")
|
| 30 |
+
input_question = gr.Textbox(label="Enter question (for Q&A only)", visible=False)
|
| 31 |
+
output = gr.Textbox(label="Output")
|
| 32 |
+
|
| 33 |
+
def process(task_choice, text, question):
|
| 34 |
+
if task_choice == "Summarizer":
|
| 35 |
+
return summarize_text(text)
|
| 36 |
+
elif task_choice == "Q&A Bot":
|
| 37 |
+
return answer_question(text, question)
|
| 38 |
+
elif task_choice == "Story Generator":
|
| 39 |
+
return generate_story(text)
|
| 40 |
+
|
| 41 |
+
task.change(lambda t: gr.update(visible=(t=="Q&A Bot")), inputs=task, outputs=input_question)
|
| 42 |
+
btn = gr.Button("Run")
|
| 43 |
+
btn.click(process, inputs=[task, input_text, input_question], outputs=output)
|
| 44 |
+
|
| 45 |
+
demo.launch()
|
| 46 |
+
|