nashjiwani commited on
Commit
876d278
ยท
verified ยท
1 Parent(s): fe17a97

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -19
app.py CHANGED
@@ -4,15 +4,15 @@ 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
 
@@ -24,40 +24,60 @@ qwen_pipe = pipeline(
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()
 
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()