Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,42 +1,51 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
|
| 4 |
-
# Load
|
| 5 |
-
tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-
|
| 6 |
-
model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-
|
| 7 |
|
| 8 |
def generate_notes(topic):
|
| 9 |
"""
|
| 10 |
-
|
| 11 |
"""
|
| 12 |
if not topic.strip():
|
| 13 |
return "⚠️ Please enter a topic first."
|
| 14 |
|
| 15 |
prompt = (
|
| 16 |
-
f"
|
| 17 |
-
f"Include
|
| 18 |
-
f"
|
|
|
|
| 19 |
)
|
| 20 |
|
|
|
|
| 21 |
inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
notes = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 24 |
-
return notes
|
| 25 |
|
| 26 |
# Gradio UI
|
| 27 |
iface = gr.Interface(
|
| 28 |
fn=generate_notes,
|
| 29 |
-
inputs=gr.Textbox(lines=2, placeholder="Enter a topic (e.g. Photosynthesis,
|
| 30 |
-
outputs=gr.Textbox(label="Generated Notes", lines=
|
| 31 |
-
title="📘 AI Note Generator",
|
| 32 |
-
description=(
|
| 33 |
-
"Enter any topic and get concise, well-structured notes generated by "
|
| 34 |
-
"a free open-source model (FLAN-T5-small)."
|
| 35 |
-
),
|
| 36 |
examples=[
|
| 37 |
-
["
|
| 38 |
-
["
|
| 39 |
-
["
|
| 40 |
]
|
| 41 |
)
|
| 42 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
|
| 4 |
+
# Load model
|
| 5 |
+
tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-large")
|
| 6 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-large")
|
| 7 |
|
| 8 |
def generate_notes(topic):
|
| 9 |
"""
|
| 10 |
+
Generate detailed paragraph-style study notes for a given topic.
|
| 11 |
"""
|
| 12 |
if not topic.strip():
|
| 13 |
return "⚠️ Please enter a topic first."
|
| 14 |
|
| 15 |
prompt = (
|
| 16 |
+
f"Write detailed, well-structured study notes about the topic '{topic}'. "
|
| 17 |
+
f"Include an introduction, key concepts, examples, and a conclusion. "
|
| 18 |
+
f"Make the notes clear, paragraph-based, and easy for a student to understand. "
|
| 19 |
+
f"Use complete sentences and proper formatting."
|
| 20 |
)
|
| 21 |
|
| 22 |
+
# Encode input
|
| 23 |
inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
|
| 24 |
+
|
| 25 |
+
# Generate long, detailed text
|
| 26 |
+
outputs = model.generate(
|
| 27 |
+
**inputs,
|
| 28 |
+
max_new_tokens=600, # increase for longer output
|
| 29 |
+
temperature=0.8, # adds variety
|
| 30 |
+
top_p=0.9, # nucleus sampling
|
| 31 |
+
repetition_penalty=1.1, # prevents repeating phrases
|
| 32 |
+
do_sample=True # enables more creative generation
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
notes = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 36 |
+
return notes.strip()
|
| 37 |
|
| 38 |
# Gradio UI
|
| 39 |
iface = gr.Interface(
|
| 40 |
fn=generate_notes,
|
| 41 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter a topic (e.g. Photosynthesis, Machine Learning)"),
|
| 42 |
+
outputs=gr.Textbox(label="Generated Detailed Notes", lines=15),
|
| 43 |
+
title="📘 AI Detailed Note Generator",
|
| 44 |
+
description="Generate detailed, structured notes with multiple paragraphs using FLAN-T5-large (runs free on CPU).",
|
|
|
|
|
|
|
|
|
|
| 45 |
examples=[
|
| 46 |
+
["Quantum computing"],
|
| 47 |
+
["Climate change and its effects"],
|
| 48 |
+
["Photosynthesis process"]
|
| 49 |
]
|
| 50 |
)
|
| 51 |
|