riyans98 commited on
Commit
7836e24
·
verified ·
1 Parent(s): fa106ae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -34
app.py CHANGED
@@ -1,18 +1,9 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
- from transformers import VitsModel, AutoTokenizer
4
- import torch
5
- import numpy as np
6
- import io
7
- import soundfile as sf
8
-
9
- # Load TTS model once at startup for efficiency
10
- tts_model = VitsModel.from_pretrained("facebook/mms-tts-hne")
11
- tts_tokenizer = AutoTokenizer.from_pretrained("facebook/mms-tts-hne")
12
 
13
  def respond(
14
  message,
15
- history: list,
16
  system_message,
17
  max_tokens,
18
  temperature,
@@ -24,43 +15,31 @@ def respond(
24
  """
25
  client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
26
  messages = [{"role": "system", "content": system_message}]
27
- # Convert history to OpenAI format (assuming history is list of [user_str, bot_str] pairs)
28
- for user_msg, bot_msg in history:
29
- if user_msg:
30
- messages.append({"role": "user", "content": user_msg})
31
- if bot_msg:
32
- messages.append({"role": "assistant", "content": bot_msg})
33
  messages.append({"role": "user", "content": message})
34
 
 
35
  # Stream the text response
36
  response = ""
37
- for chunk in client.chat_completion(
 
 
38
  messages,
39
  max_tokens=max_tokens,
40
  stream=True,
41
  temperature=temperature,
42
  top_p=top_p,
43
  ):
44
- choices = chunk.choices
45
  token = ""
46
  if len(choices) and choices[0].delta.content:
47
  token = choices[0].delta.content
48
- response += token
49
- # Yield partial text update to the chatbot (bot_msg is str for now)
50
- yield history + [[message, response]], None # None for audio placeholder
51
-
52
- # After full text is generated, create TTS audio
53
- inputs = tts_tokenizer(response, return_tensors="pt")
54
- with torch.no_grad():
55
- waveform = tts_model(**inputs).waveform.squeeze().cpu().numpy()
56
-
57
- # Convert waveform to bytes (WAV format) for Gradio Audio
58
- buffer = io.BytesIO()
59
- sf.write(buffer, waveform, tts_model.config.sampling_rate, format='wav')
60
- audio_bytes = buffer.getvalue()
61
 
62
- # Yield final update: chatbot with full text, and audio bytes
63
- yield history + [[message, response]], audio_bytes
 
64
 
65
  with gr.Blocks() as demo:
66
  with gr.Row():
@@ -68,7 +47,7 @@ with gr.Blocks() as demo:
68
  gr.LoginButton(label="Login with Hugging Face")
69
  with gr.Column(scale=4):
70
  chatbot = gr.Chatbot(height=500)
71
- audio_output = gr.Audio(label="Play TTS Audio (Chhattisgarhi)", interactive=False)
72
  msg = gr.Textbox(label="Your message")
73
  submit_btn = gr.Button("Send")
74
 
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
 
 
 
 
 
 
 
 
 
3
 
4
  def respond(
5
  message,
6
+ history: list[dict[str, str]],
7
  system_message,
8
  max_tokens,
9
  temperature,
 
15
  """
16
  client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
17
  messages = [{"role": "system", "content": system_message}]
18
+
19
+ messages.extend(history)
20
+
 
 
 
21
  messages.append({"role": "user", "content": message})
22
 
23
+
24
  # Stream the text response
25
  response = ""
26
+
27
+
28
+ for message in client.chat_completion(
29
  messages,
30
  max_tokens=max_tokens,
31
  stream=True,
32
  temperature=temperature,
33
  top_p=top_p,
34
  ):
35
+ choices = message.choices
36
  token = ""
37
  if len(choices) and choices[0].delta.content:
38
  token = choices[0].delta.content
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
+ response += token
41
+ yield response
42
+
43
 
44
  with gr.Blocks() as demo:
45
  with gr.Row():
 
47
  gr.LoginButton(label="Login with Hugging Face")
48
  with gr.Column(scale=4):
49
  chatbot = gr.Chatbot(height=500)
50
+
51
  msg = gr.Textbox(label="Your message")
52
  submit_btn = gr.Button("Send")
53