ajsbsd commited on
Commit
212bb71
·
verified ·
1 Parent(s): 2cb4d47

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -20
app.py CHANGED
@@ -1,13 +1,9 @@
1
  import gradio as gr
2
  import torch
3
  from transformers import (
4
- AutoTokenizer,
5
- AutoModelForCausalLM,
6
- SpeechT5Processor,
7
- SpeechT5ForTextToSpeech,
8
- SpeechT5HifiGan,
9
- WhisperProcessor,
10
- WhisperForConditionalGeneration
11
  )
12
  from datasets import load_dataset
13
  import os
@@ -17,7 +13,7 @@ import soundfile as sf
17
  import librosa
18
  import yaml
19
 
20
- # --- Configuration ---
21
  HUGGINGFACE_MODEL_ID = "HuggingFaceH4/Qwen2.5-1.5B-Instruct-gkd"
22
  TORCH_DTYPE = torch.bfloat16
23
  MAX_NEW_TOKENS = 512
@@ -30,7 +26,7 @@ TTS_MODEL_ID = "microsoft/speecht5_tts"
30
  TTS_VOCODER_ID = "microsoft/speecht5_hifigan"
31
  STT_MODEL_ID = "openai/whisper-small"
32
 
33
- # --- Global Variables ---
34
  tokenizer = None
35
  llm_model = None
36
  tts_processor = None
@@ -41,9 +37,12 @@ whisper_processor = None
41
  whisper_model = None
42
  first_load = True
43
 
 
44
  def generate_pretty_html(data):
45
  html = """
46
- <div style="font-family: Arial, sans-serif; max-width: 600px; margin: auto; background-color: #f9f9f9; border-radius: 10px; padding: 20px; box-shadow: 0 4px 12px rgba(0,0,0,0.1);">
 
 
47
  <h2 style="color: #2c3e50; border-bottom: 2px solid #ddd; padding-bottom: 10px;">Model Info</h2>
48
  """
49
  for key, value in data.items():
@@ -61,14 +60,17 @@ def load_config():
61
  return yaml.safe_load(f)
62
 
63
  def render_modern_info():
64
- config = load_config()
65
- return generate_pretty_html(config)
 
 
 
66
 
67
  def load_readme():
68
  with open("README.md", "r", encoding="utf-8") as f:
69
  return f.read()
70
 
71
- # --- Helper: Split Text Into Chunks ---
72
  def split_text_into_chunks(text, max_chars=400):
73
  sentences = text.replace("...", ".").split(". ")
74
  chunks = []
@@ -83,7 +85,7 @@ def split_text_into_chunks(text, max_chars=400):
83
  chunks.append(current_chunk)
84
  return [f"{chunk}." for chunk in chunks if chunk.strip()]
85
 
86
- # --- Load Models Function ---
87
  @spaces.GPU
88
  def load_models():
89
  global tokenizer, llm_model, tts_processor, tts_model, tts_vocoder, speaker_embeddings, whisper_processor, whisper_model
@@ -132,7 +134,7 @@ def load_models():
132
  except Exception as e:
133
  print(f"Error loading Whisper: {e}")
134
 
135
- # --- Generate Response and Audio ---
136
  @spaces.GPU
137
  def generate_response_and_audio(message, history):
138
  global first_load
@@ -196,7 +198,6 @@ def generate_response_and_audio(message, history):
196
 
197
  return history + [{"role": "assistant", "content": generated_text}], audio_path
198
 
199
- # --- Transcribe Audio ---
200
  @spaces.GPU
201
  def transcribe_audio(filepath):
202
  global first_load
@@ -216,7 +217,7 @@ def transcribe_audio(filepath):
216
  except Exception as e:
217
  return f"Transcription failed: {e}"
218
 
219
- # --- Gradio UI ---
220
  with gr.Blocks() as demo:
221
  gr.Markdown("# Qwen2.5 Chatbot with Voice Input/Output")
222
 
@@ -233,9 +234,15 @@ with gr.Blocks() as demo:
233
 
234
  clear_btn = gr.Button("Clear All")
235
  clear_btn.click(lambda: ([], "", None), None, [chatbot, text_input, audio_output])
236
- #gr.Markdown("---")
237
  gr.Markdown(load_readme())
238
  gr.Markdown("---")
239
- #html_output = gr.HTML()
240
 
241
- demo.load(fn=render_modern_info, outputs=html_output)
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import torch
3
  from transformers import (
4
+ AutoTokenizer, AutoModelForCausalLM,
5
+ SpeechT5Processor, SpeechT5ForTextToSpeech, SpeechT5HifiGan,
6
+ WhisperProcessor, WhisperForConditionalGeneration
 
 
 
 
7
  )
8
  from datasets import load_dataset
9
  import os
 
13
  import librosa
14
  import yaml
15
 
16
+ # ================== Configuration ==================
17
  HUGGINGFACE_MODEL_ID = "HuggingFaceH4/Qwen2.5-1.5B-Instruct-gkd"
18
  TORCH_DTYPE = torch.bfloat16
19
  MAX_NEW_TOKENS = 512
 
26
  TTS_VOCODER_ID = "microsoft/speecht5_hifigan"
27
  STT_MODEL_ID = "openai/whisper-small"
28
 
29
+ # ================== Global Variables ==================
30
  tokenizer = None
31
  llm_model = None
32
  tts_processor = None
 
37
  whisper_model = None
38
  first_load = True
39
 
40
+ # ================== UI Helpers ==================
41
  def generate_pretty_html(data):
42
  html = """
43
+ <div style="font-family: Arial, sans-serif; max-width: 600px; margin: auto;
44
+ background-color: #f9f9f9; border-radius: 10px; padding: 20px;
45
+ box-shadow: 0 4px 12px rgba(0,0,0,0.1);">
46
  <h2 style="color: #2c3e50; border-bottom: 2px solid #ddd; padding-bottom: 10px;">Model Info</h2>
47
  """
48
  for key, value in data.items():
 
60
  return yaml.safe_load(f)
61
 
62
  def render_modern_info():
63
+ try:
64
+ config = load_config()
65
+ return generate_pretty_html(config)
66
+ except Exception as e:
67
+ return f"<div style='color: red;'>Error loading config: {str(e)}</div>"
68
 
69
  def load_readme():
70
  with open("README.md", "r", encoding="utf-8") as f:
71
  return f.read()
72
 
73
+ # ================== Helper Functions ==================
74
  def split_text_into_chunks(text, max_chars=400):
75
  sentences = text.replace("...", ".").split(". ")
76
  chunks = []
 
85
  chunks.append(current_chunk)
86
  return [f"{chunk}." for chunk in chunks if chunk.strip()]
87
 
88
+ # ================== Model Loading ==================
89
  @spaces.GPU
90
  def load_models():
91
  global tokenizer, llm_model, tts_processor, tts_model, tts_vocoder, speaker_embeddings, whisper_processor, whisper_model
 
134
  except Exception as e:
135
  print(f"Error loading Whisper: {e}")
136
 
137
+ # ================== Chat & Audio Functions ==================
138
  @spaces.GPU
139
  def generate_response_and_audio(message, history):
140
  global first_load
 
198
 
199
  return history + [{"role": "assistant", "content": generated_text}], audio_path
200
 
 
201
  @spaces.GPU
202
  def transcribe_audio(filepath):
203
  global first_load
 
217
  except Exception as e:
218
  return f"Transcription failed: {e}"
219
 
220
+ # ================== Gradio UI ==================
221
  with gr.Blocks() as demo:
222
  gr.Markdown("# Qwen2.5 Chatbot with Voice Input/Output")
223
 
 
234
 
235
  clear_btn = gr.Button("Clear All")
236
  clear_btn.click(lambda: ([], "", None), None, [chatbot, text_input, audio_output])
237
+
238
  gr.Markdown(load_readme())
239
  gr.Markdown("---")
 
240
 
241
+ # ✅ Define html_output BEFORE using it
242
+ html_output = gr.HTML("<div style='text-align:center; padding: 20px;'>Loading model info...</div>")
243
+
244
+ # ✅ Now this works!
245
+ demo.load(fn=render_modern_info, outputs=html_output)
246
+
247
+ # ================== Launch App ==================
248
+ demo.queue().launch()