Update app.py
Browse files
app.py
CHANGED
|
@@ -1,57 +1,49 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
| 4 |
-
from diffusers import StableDiffusionPipeline
|
| 5 |
import torch
|
| 6 |
|
| 7 |
-
# Load
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
torch_dtype=torch.float16
|
| 13 |
-
|
| 14 |
-
)
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
temperature=0.95,
|
| 26 |
-
do_sample=True,
|
| 27 |
-
pad_token_id=tokenizer.eos_token_id
|
| 28 |
-
)
|
| 29 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
| 34 |
|
| 35 |
with gr.Blocks(theme="soft") as demo:
|
| 36 |
-
gr.Markdown("##
|
| 37 |
|
| 38 |
with gr.Row():
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
image = generate_image(f"Surrealistic {selected_theme}")
|
| 48 |
-
return story, image
|
| 49 |
|
| 50 |
-
generate_btn = gr.Button("✨ Generate Random Experience")
|
| 51 |
generate_btn.click(
|
| 52 |
-
fn=
|
| 53 |
-
inputs=
|
| 54 |
-
outputs=
|
| 55 |
)
|
| 56 |
|
| 57 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
|
|
|
|
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
# Load tiniest possible model (24M parameters)
|
| 6 |
+
model_name = "microsoft/phi-1_5"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
+
model_name,
|
| 10 |
+
torch_dtype=torch.float16,
|
| 11 |
+
device_map="auto"
|
| 12 |
+
)
|
| 13 |
|
| 14 |
+
def generate(prompt):
|
| 15 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 16 |
+
with torch.inference_mode():
|
| 17 |
+
outputs = model.generate(
|
| 18 |
+
**inputs,
|
| 19 |
+
max_new_tokens=30,
|
| 20 |
+
temperature=0.7,
|
| 21 |
+
do_sample=True
|
| 22 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 24 |
|
| 25 |
+
# UI components
|
| 26 |
+
emoji = "🔮"
|
| 27 |
+
title = "Tiny Fortune Cookie Generator"
|
| 28 |
+
description = "Uses Microsoft Phi-1.5 (24M params) to generate micro-wisdom"
|
| 29 |
|
| 30 |
with gr.Blocks(theme="soft") as demo:
|
| 31 |
+
gr.Markdown(f"## {emoji} {title}\n{description}")
|
| 32 |
|
| 33 |
with gr.Row():
|
| 34 |
+
prompt_input = gr.Textbox(
|
| 35 |
+
value="What's my fortune today?",
|
| 36 |
+
label="Input Prompt",
|
| 37 |
+
placeholder="Ask your question..."
|
| 38 |
+
)
|
| 39 |
|
| 40 |
+
output_text = gr.Textbox(label="Your Fortune")
|
| 41 |
+
generate_btn = gr.Button("Open Cookie 🍪", variant="primary")
|
|
|
|
|
|
|
| 42 |
|
|
|
|
| 43 |
generate_btn.click(
|
| 44 |
+
fn=generate,
|
| 45 |
+
inputs=prompt_input,
|
| 46 |
+
outputs=output_text
|
| 47 |
)
|
| 48 |
|
| 49 |
demo.launch()
|