Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,45 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import AutoTokenizer,
|
| 3 |
|
| 4 |
-
# Load
|
| 5 |
-
|
| 6 |
-
|
|
|
|
| 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"Write detailed, well-structured study notes about
|
| 17 |
-
f"Include an introduction,
|
| 18 |
-
f"
|
| 19 |
-
f"Use complete sentences and proper formatting."
|
| 20 |
)
|
| 21 |
|
| 22 |
-
|
| 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,
|
| 29 |
-
temperature=0.
|
| 30 |
-
top_p=0.9,
|
| 31 |
-
repetition_penalty=1.
|
| 32 |
-
do_sample=True # enables more creative generation
|
| 33 |
)
|
| 34 |
|
| 35 |
-
|
| 36 |
-
return
|
| 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,
|
| 42 |
-
outputs=gr.Textbox(label="Generated
|
| 43 |
-
title="
|
| 44 |
-
description="Generate
|
| 45 |
examples=[
|
| 46 |
-
["
|
| 47 |
-
["
|
| 48 |
-
["
|
|
|
|
| 49 |
]
|
| 50 |
)
|
| 51 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
|
| 4 |
+
# Load TinyLlama (small but smart)
|
| 5 |
+
model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 7 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 8 |
|
| 9 |
def generate_notes(topic):
|
| 10 |
+
topic = topic.strip()
|
| 11 |
+
if not topic:
|
|
|
|
|
|
|
| 12 |
return "⚠️ Please enter a topic first."
|
| 13 |
|
| 14 |
prompt = (
|
| 15 |
+
f"You are an expert teacher. Write detailed, well-structured study notes about '{topic}'. "
|
| 16 |
+
f"Include an introduction, main concepts, key points, and a short summary. "
|
| 17 |
+
f"Keep the tone educational and clear for students."
|
|
|
|
| 18 |
)
|
| 19 |
|
| 20 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
|
|
|
|
|
|
|
|
|
| 21 |
outputs = model.generate(
|
| 22 |
**inputs,
|
| 23 |
+
max_new_tokens=600,
|
| 24 |
+
temperature=0.7,
|
| 25 |
+
top_p=0.9,
|
| 26 |
+
repetition_penalty=1.2,
|
|
|
|
| 27 |
)
|
| 28 |
|
| 29 |
+
text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 30 |
+
return text.replace(prompt, "").strip()
|
| 31 |
|
|
|
|
| 32 |
iface = gr.Interface(
|
| 33 |
fn=generate_notes,
|
| 34 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter a topic (e.g. Photosynthesis, World Geography)"),
|
| 35 |
+
outputs=gr.Textbox(lines=15, label="Generated Notes"),
|
| 36 |
+
title="📚 AI Note Generator (TinyLlama 1.1B Chat)",
|
| 37 |
+
description="Generate long, educational notes for free using the TinyLlama 1.1B Chat model.",
|
| 38 |
examples=[
|
| 39 |
+
["Photosynthesis process"],
|
| 40 |
+
["Causes of World War II"],
|
| 41 |
+
["Artificial Intelligence"],
|
| 42 |
+
["The Water Cycle"]
|
| 43 |
]
|
| 44 |
)
|
| 45 |
|