Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,15 +4,15 @@ import torch
|
|
| 4 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 5 |
|
| 6 |
# -------------------------------
|
| 7 |
-
# Load Qwen2.5-7B
|
| 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,
|
| 15 |
-
device_map="auto",
|
| 16 |
trust_remote_code=True
|
| 17 |
)
|
| 18 |
|
|
@@ -24,40 +24,60 @@ qwen_pipe = pipeline(
|
|
| 24 |
)
|
| 25 |
|
| 26 |
# -------------------------------
|
| 27 |
-
# Life Genie
|
| 28 |
# -------------------------------
|
| 29 |
-
@spaces.GPU
|
| 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, #
|
| 47 |
font-family: Trebuchet MS, sans-serif;
|
| 48 |
}
|
| 49 |
""") as demo:
|
| 50 |
|
| 51 |
-
gr.HTML("<h1 style='text-align:center; color:white;'
|
| 52 |
-
gr.Markdown("
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
demo.launch()
|
|
|
|
| 4 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 5 |
|
| 6 |
# -------------------------------
|
| 7 |
+
# Load Qwen2.5-7B (text-only model, works for both Genie & Wizard replies)
|
| 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,
|
| 15 |
+
device_map="auto",
|
| 16 |
trust_remote_code=True
|
| 17 |
)
|
| 18 |
|
|
|
|
| 24 |
)
|
| 25 |
|
| 26 |
# -------------------------------
|
| 27 |
+
# Life Genie: text chat
|
| 28 |
# -------------------------------
|
| 29 |
+
@spaces.GPU
|
| 30 |
def life_genie(user_input: str) -> str:
|
|
|
|
|
|
|
|
|
|
| 31 |
if not user_input.strip():
|
| 32 |
return "๐ง Please ask me something magical!"
|
|
|
|
| 33 |
prompt = f"You are a witty magical genie. Answer creatively.\nUser: {user_input}\nGenie:"
|
| 34 |
output = qwen_pipe(prompt, max_new_tokens=256, do_sample=True, temperature=0.7)
|
| 35 |
return output[0]["generated_text"]
|
| 36 |
|
| 37 |
+
# -------------------------------
|
| 38 |
+
# Whisper ASR for voice input
|
| 39 |
+
# -------------------------------
|
| 40 |
+
asr_pipe = pipeline("automatic-speech-recognition", model="openai/whisper-tiny", device=-1)
|
| 41 |
+
|
| 42 |
+
# -------------------------------
|
| 43 |
+
# Voice Wizard: speech โ Genie reply
|
| 44 |
+
# -------------------------------
|
| 45 |
+
@spaces.GPU
|
| 46 |
+
def voice_wizard(audio_file) -> str:
|
| 47 |
+
if audio_file is None:
|
| 48 |
+
return "๐ค Please record your voice first!"
|
| 49 |
+
# Step 1: Transcribe microphone audio
|
| 50 |
+
transcript = asr_pipe(audio_file)["text"]
|
| 51 |
+
# Step 2: Ask Genie with that transcript
|
| 52 |
+
prompt = f"The user spoke: '{transcript}'. Respond like a magical wizard with wisdom and humor.\nWizard:"
|
| 53 |
+
output = qwen_pipe(prompt, max_new_tokens=256, do_sample=True, temperature=0.7)
|
| 54 |
+
return f"โจ Voice Transcription: {transcript}\n\n๐ฎ Wizard Replies:\n{output[0]['generated_text']}"
|
| 55 |
+
|
| 56 |
# -------------------------------
|
| 57 |
# Gradio UI
|
| 58 |
# -------------------------------
|
| 59 |
with gr.Blocks(css="""
|
| 60 |
body {
|
| 61 |
+
background: linear-gradient(135deg, #ff9a9e, #fad0c4);
|
| 62 |
font-family: Trebuchet MS, sans-serif;
|
| 63 |
}
|
| 64 |
""") as demo:
|
| 65 |
|
| 66 |
+
gr.HTML("<h1 style='text-align:center; color:white;'>๐โจ Magic-Lab โจ๐</h1>")
|
| 67 |
+
gr.Markdown("Welcome! Meet the Genie through text or voice. ๐ง๐ฎ")
|
| 68 |
|
| 69 |
+
# Life Genie (Text)
|
| 70 |
+
with gr.Tab("๐ง Life Genie"):
|
| 71 |
+
txt_in = gr.Textbox(lines=3, placeholder="Ask me anything...", label="Your Wish")
|
| 72 |
+
txt_out = gr.Textbox(lines=10, label="Genie's Response")
|
| 73 |
+
btn_chat = gr.Button("โจ Summon Genie")
|
| 74 |
+
btn_chat.click(life_genie, inputs=txt_in, outputs=txt_out)
|
| 75 |
|
| 76 |
+
# Voice Wizard (Voice)
|
| 77 |
+
with gr.Tab("๐ฎ Voice Wizard"):
|
| 78 |
+
aud_in = gr.Audio(sources=["microphone"], type="filepath", label="Speak your spell ๐ค")
|
| 79 |
+
aud_out = gr.Textbox(lines=10, label="Wizard's Reply")
|
| 80 |
+
btn_voice = gr.Button("๐๏ธ Cast Spell")
|
| 81 |
+
btn_voice.click(voice_wizard, inputs=aud_in, outputs=aud_out)
|
| 82 |
|
| 83 |
demo.launch()
|