sagar007 commited on
Commit
ae87ff0
·
verified ·
1 Parent(s): c8be55b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -11
app.py CHANGED
@@ -13,8 +13,8 @@ import soundfile as sf
13
  subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
14
 
15
  # Constants
16
- TITLE = "<h1><center>Phi 3.5 Multimodal (Text + Vision)</center></h1>"
17
- DESCRIPTION = "# Phi-3.5 Multimodal Demo (Text + Vision)"
18
 
19
  # Model configurations
20
  TEXT_MODEL_ID = "microsoft/Phi-3.5-mini-instruct"
@@ -48,6 +48,11 @@ vision_model = AutoModelForCausalLM.from_pretrained(
48
 
49
  vision_processor = AutoProcessor.from_pretrained(VISION_MODEL_ID, trust_remote_code=True)
50
 
 
 
 
 
 
51
  # Helper functions
52
  @spaces.GPU
53
  def stream_text_chat(message, history, system_prompt, temperature=0.8, max_new_tokens=1024, top_p=1.0, top_k=20):
@@ -82,7 +87,7 @@ def stream_text_chat(message, history, system_prompt, temperature=0.8, max_new_t
82
  buffer += new_text
83
  yield history + [[message, buffer]]
84
 
85
- @spaces.GPU # Add this decorator
86
  def process_vision_query(image, text_input):
87
  prompt = f"<|user|>\n<|image_1|>\n{text_input}<|end|>\n<|assistant|>\n"
88
  image = Image.fromarray(image).convert("RGB")
@@ -98,11 +103,6 @@ def process_vision_query(image, text_input):
98
  generate_ids = generate_ids[:, inputs['input_ids'].shape[1]:]
99
  response = vision_processor.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
100
  return response
101
-
102
- # Load Parler-TTS model
103
- tts_device = "cuda:0" if torch.cuda.is_available() else "cpu"
104
- tts_model = ParlerTTSForConditionalGeneration.from_pretrained("parler-tts/parler-tts-large-v1").to(tts_device)
105
- tts_tokenizer = AutoTokenizer.from_pretrained("parler-tts/parler-tts-large-v1")
106
 
107
  @spaces.GPU
108
  def generate_speech(prompt, description):
@@ -138,7 +138,7 @@ footer { text-align: center; margin-top: 2rem; color: #64748b;}
138
  custom_header = """
139
  <div id="custom-header">
140
  <h1><span class="blue">Phi 3.5</span> <span class="pink">Multimodal Assistant</span></h1>
141
- <h2>Text and Vision AI at Your Service</h2>
142
  </div>
143
  """
144
 
@@ -178,10 +178,31 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Base().set(
178
  gr.HTML(custom_suggestions)
179
 
180
  with gr.Tab("Text Model (Phi-3.5-mini)"):
181
- # ... (previous text model code remains the same)
 
 
 
 
 
 
 
 
 
 
 
 
 
182
 
183
  with gr.Tab("Vision Model (Phi-3.5-vision)"):
184
- # ... (previous vision model code remains the same)
 
 
 
 
 
 
 
 
185
 
186
  with gr.Tab("Text-to-Speech (Parler-TTS)"):
187
  with gr.Row():
 
13
  subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
14
 
15
  # Constants
16
+ TITLE = "<h1><center>Phi 3.5 Multimodal (Text + Vision + Speech)</center></h1>"
17
+ DESCRIPTION = "# Phi-3.5 Multimodal Demo (Text + Vision + Speech)"
18
 
19
  # Model configurations
20
  TEXT_MODEL_ID = "microsoft/Phi-3.5-mini-instruct"
 
48
 
49
  vision_processor = AutoProcessor.from_pretrained(VISION_MODEL_ID, trust_remote_code=True)
50
 
51
+ # Load Parler-TTS model
52
+ tts_device = "cuda:0" if torch.cuda.is_available() else "cpu"
53
+ tts_model = ParlerTTSForConditionalGeneration.from_pretrained("parler-tts/parler-tts-large-v1").to(tts_device)
54
+ tts_tokenizer = AutoTokenizer.from_pretrained("parler-tts/parler-tts-large-v1")
55
+
56
  # Helper functions
57
  @spaces.GPU
58
  def stream_text_chat(message, history, system_prompt, temperature=0.8, max_new_tokens=1024, top_p=1.0, top_k=20):
 
87
  buffer += new_text
88
  yield history + [[message, buffer]]
89
 
90
+ @spaces.GPU
91
  def process_vision_query(image, text_input):
92
  prompt = f"<|user|>\n<|image_1|>\n{text_input}<|end|>\n<|assistant|>\n"
93
  image = Image.fromarray(image).convert("RGB")
 
103
  generate_ids = generate_ids[:, inputs['input_ids'].shape[1]:]
104
  response = vision_processor.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
105
  return response
 
 
 
 
 
106
 
107
  @spaces.GPU
108
  def generate_speech(prompt, description):
 
138
  custom_header = """
139
  <div id="custom-header">
140
  <h1><span class="blue">Phi 3.5</span> <span class="pink">Multimodal Assistant</span></h1>
141
+ <h2>Text, Vision, and Speech AI at Your Service</h2>
142
  </div>
143
  """
144
 
 
178
  gr.HTML(custom_suggestions)
179
 
180
  with gr.Tab("Text Model (Phi-3.5-mini)"):
181
+ chatbot = gr.Chatbot(height=400)
182
+ msg = gr.Textbox(label="Message", placeholder="Type your message here...")
183
+ with gr.Accordion("Advanced Options", open=False):
184
+ system_prompt = gr.Textbox(value="You are a helpful assistant", label="System Prompt")
185
+ temperature = gr.Slider(minimum=0, maximum=1, step=0.1, value=0.8, label="Temperature")
186
+ max_new_tokens = gr.Slider(minimum=128, maximum=8192, step=1, value=1024, label="Max new tokens")
187
+ top_p = gr.Slider(minimum=0.0, maximum=1.0, step=0.1, value=1.0, label="top_p")
188
+ top_k = gr.Slider(minimum=1, maximum=20, step=1, value=20, label="top_k")
189
+
190
+ submit_btn = gr.Button("Submit", variant="primary")
191
+ clear_btn = gr.Button("Clear Chat", variant="secondary")
192
+
193
+ submit_btn.click(stream_text_chat, [msg, chatbot, system_prompt, temperature, max_new_tokens, top_p, top_k], [chatbot])
194
+ clear_btn.click(lambda: None, None, chatbot, queue=False)
195
 
196
  with gr.Tab("Vision Model (Phi-3.5-vision)"):
197
+ with gr.Row():
198
+ with gr.Column(scale=1):
199
+ vision_input_img = gr.Image(label="Upload an Image", type="pil")
200
+ vision_text_input = gr.Textbox(label="Ask a question about the image", placeholder="What do you see in this image?")
201
+ vision_submit_btn = gr.Button("Analyze Image", variant="primary")
202
+ with gr.Column(scale=1):
203
+ vision_output_text = gr.Textbox(label="AI Analysis", lines=10)
204
+
205
+ vision_submit_btn.click(process_vision_query, [vision_input_img, vision_text_input], [vision_output_text])
206
 
207
  with gr.Tab("Text-to-Speech (Parler-TTS)"):
208
  with gr.Row():