Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,63 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import spaces
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
demo = gr.Interface(fn=echo, inputs="text", outputs="text")
|
| 10 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import spaces
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 5 |
|
| 6 |
+
# -------------------------------
|
| 7 |
+
# Load Qwen2.5-7B for text-only chat
|
| 8 |
+
# -------------------------------
|
| 9 |
+
model_id = "Qwen/Qwen2.5-7B-Instruct"
|
| 10 |
+
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
| 12 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 13 |
+
model_id,
|
| 14 |
+
torch_dtype=torch.float16, # use half precision for GPU memory
|
| 15 |
+
device_map="auto", # map layers automatically to GPU
|
| 16 |
+
trust_remote_code=True
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
qwen_pipe = pipeline(
|
| 20 |
+
"text-generation",
|
| 21 |
+
model=model,
|
| 22 |
+
tokenizer=tokenizer,
|
| 23 |
+
device_map="auto"
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# -------------------------------
|
| 27 |
+
# Life Genie function
|
| 28 |
+
# -------------------------------
|
| 29 |
+
@spaces.GPU # ensure HF detects GPU usage
|
| 30 |
+
def life_genie(user_input: str) -> str:
|
| 31 |
+
"""
|
| 32 |
+
Life Genie: magical assistant powered by Qwen2.5-7B.
|
| 33 |
+
"""
|
| 34 |
+
if not user_input.strip():
|
| 35 |
+
return "🧞 Please ask me something magical!"
|
| 36 |
+
|
| 37 |
+
prompt = f"You are a witty magical genie. Answer creatively.\nUser: {user_input}\nGenie:"
|
| 38 |
+
output = qwen_pipe(prompt, max_new_tokens=256, do_sample=True, temperature=0.7)
|
| 39 |
+
return output[0]["generated_text"]
|
| 40 |
+
|
| 41 |
+
# -------------------------------
|
| 42 |
+
# Gradio UI
|
| 43 |
+
# -------------------------------
|
| 44 |
+
with gr.Blocks(css="""
|
| 45 |
+
body {
|
| 46 |
+
background: linear-gradient(135deg, #ff9966, #ff5e62);
|
| 47 |
+
font-family: Trebuchet MS, sans-serif;
|
| 48 |
+
}
|
| 49 |
+
""") as demo:
|
| 50 |
+
|
| 51 |
+
gr.HTML("<h1 style='text-align:center; color:white;'>🧞✨ Life Genie — Ask Your Magical Assistant ✨🧞</h1>")
|
| 52 |
+
gr.Markdown("Type your wish below and summon the genie for a response!")
|
| 53 |
+
|
| 54 |
+
with gr.Row():
|
| 55 |
+
with gr.Column(scale=1):
|
| 56 |
+
user_box = gr.Textbox(lines=3, placeholder="E.g. Cheer me up about Mondays 🪄", label="Your Wish")
|
| 57 |
+
submit_btn = gr.Button("✨ Summon Genie")
|
| 58 |
+
with gr.Column(scale=1):
|
| 59 |
+
genie_out = gr.Textbox(lines=10, label="Genie Responds")
|
| 60 |
+
|
| 61 |
+
submit_btn.click(life_genie, inputs=user_box, outputs=genie_out)
|
| 62 |
|
|
|
|
| 63 |
demo.launch()
|