Upload folder using huggingface_hub
Browse files- app.py +58 -0
- requirements.txt +6 -0
app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Load model
|
| 6 |
+
print("Loading text generation model...")
|
| 7 |
+
model_id = "Yehia-Elsawy/smollm-360m-stories-mlx"
|
| 8 |
+
|
| 9 |
+
try:
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 11 |
+
model = AutoModelForCausalLM.from_pretrained(model_id)
|
| 12 |
+
print("✅ Model loaded!")
|
| 13 |
+
except Exception as e:
|
| 14 |
+
print(f"❌ Error loading model: {e}")
|
| 15 |
+
raise e
|
| 16 |
+
|
| 17 |
+
def generate_story(prompt, max_length=200, temperature=0.8):
|
| 18 |
+
"""Generate story continuation from a prompt."""
|
| 19 |
+
|
| 20 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 21 |
+
|
| 22 |
+
with torch.no_grad():
|
| 23 |
+
outputs = model.generate(
|
| 24 |
+
**inputs,
|
| 25 |
+
max_new_tokens=max_length,
|
| 26 |
+
do_sample=True,
|
| 27 |
+
temperature=temperature,
|
| 28 |
+
top_p=0.9,
|
| 29 |
+
repetition_penalty=1.1
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 33 |
+
return generated_text
|
| 34 |
+
|
| 35 |
+
# Create interface
|
| 36 |
+
demo = gr.Interface(
|
| 37 |
+
fn=generate_story,
|
| 38 |
+
inputs=[
|
| 39 |
+
gr.Textbox(
|
| 40 |
+
lines=3,
|
| 41 |
+
placeholder="Once upon a time...",
|
| 42 |
+
label="Start your story"
|
| 43 |
+
),
|
| 44 |
+
gr.Slider(50, 300, value=150, step=10, label="Length"),
|
| 45 |
+
gr.Slider(0.5, 1.5, value=0.9, step=0.1, label="Creativity (Temperature)")
|
| 46 |
+
],
|
| 47 |
+
outputs=gr.Textbox(label="Generated Story", lines=10),
|
| 48 |
+
title="SmolLM Storyteller",
|
| 49 |
+
description="Fine-tuned on 200k stories. Give it a prompt and watch it continue your tale!",
|
| 50 |
+
examples=[
|
| 51 |
+
["Once upon a time, there was a brave little robot who", 150, 0.9],
|
| 52 |
+
["The dragon lived in a cave made of ice.", 150, 0.9],
|
| 53 |
+
["My best friend's name is Luna. She loves to", 150, 0.9]
|
| 54 |
+
]
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
if __name__ == "__main__":
|
| 58 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers>=4.40.0
|
| 2 |
+
torch
|
| 3 |
+
gradio
|
| 4 |
+
accelerate
|
| 5 |
+
protobuf
|
| 6 |
+
sentencepiece
|