Daemontatox commited on
Commit
6fb69ae
1 Parent(s): ee1262f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -33
app.py CHANGED
@@ -1,32 +1,39 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
 
 
 
3
 
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
 
 
 
 
 
 
 
 
9
 
10
  def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
  top_p,
17
  ):
18
  messages = [{"role": "system", "content": system_message}]
19
-
20
  for val in history:
21
  if val[0]:
22
  messages.append({"role": "user", "content": val[0]})
23
  if val[1]:
24
  messages.append({"role": "assistant", "content": val[1]})
25
-
26
  messages.append({"role": "user", "content": message})
27
-
28
  response = ""
29
-
30
  for message in client.chat_completion(
31
  messages,
32
  max_tokens=max_tokens,
@@ -35,30 +42,84 @@ def respond(
35
  top_p=top_p,
36
  ):
37
  token = message.choices[0].delta.content
38
-
39
  response += token
40
  yield response
 
 
 
 
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
 
 
 
 
 
 
 
 
 
 
 
61
 
 
 
 
 
 
 
 
 
 
 
62
 
63
  if __name__ == "__main__":
64
- demo.launch()
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
+ from gtts import gTTS
4
+ import os
5
+ import tempfile
6
 
7
+ """IMMY ON device Alpha version with TTS"""
 
 
 
8
 
9
+ client = InferenceClient("Daemontatox/IMMY_1f")
10
+
11
+ def text_to_speech(text):
12
+ """Convert text to speech and return the path to the audio file"""
13
+ tts = gTTS(text=text, lang='en')
14
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.mp3') as fp:
15
+ tts.save(fp.name)
16
+ return fp.name
17
 
18
  def respond(
19
+ message,
20
+ history: list[tuple[str, str]],
21
+ system_message,
22
+ max_tokens,
23
+ temperature,
24
  top_p,
25
  ):
26
  messages = [{"role": "system", "content": system_message}]
27
+
28
  for val in history:
29
  if val[0]:
30
  messages.append({"role": "user", "content": val[0]})
31
  if val[1]:
32
  messages.append({"role": "assistant", "content": val[1]})
33
+
34
  messages.append({"role": "user", "content": message})
 
35
  response = ""
36
+
37
  for message in client.chat_completion(
38
  messages,
39
  max_tokens=max_tokens,
 
42
  top_p=top_p,
43
  ):
44
  token = message.choices[0].delta.content
 
45
  response += token
46
  yield response
47
+
48
+ # Generate audio after the complete response is ready
49
+ audio_path = text_to_speech(response)
50
+ yield (response, audio_path)
51
 
52
+ with gr.Blocks() as demo:
53
+ chatbot = gr.Chatbot()
54
+ msg = gr.Textbox(label="Message")
55
+ audio_output = gr.Audio(label="Response Audio")
56
+
57
+ system_message = gr.Textbox(
58
+ value="You are Immy, a magical AI-powered teddy bear who loves chatting with children. You are kind, funny, and full of wonder, always ready to tell stories, answer questions, and offer friendly advice. Speak playfully and patiently, using simple, child-friendly language to encourage curiosity, learning, and imagination.",
59
+ label="System message"
60
+ )
61
+ max_tokens = gr.Slider(
62
+ minimum=1,
63
+ maximum=2048,
64
+ value=512,
65
+ step=1,
66
+ label="Max new tokens"
67
+ )
68
+ temperature = gr.Slider(
69
+ minimum=0.1,
70
+ maximum=4.0,
71
+ value=0.7,
72
+ step=0.1,
73
+ label="Temperature"
74
+ )
75
+ top_p = gr.Slider(
76
+ minimum=0.1,
77
+ maximum=1.0,
78
+ value=0.95,
79
+ step=0.05,
80
+ label="Top-p (nucleus sampling)"
81
+ )
82
 
83
+ def user(user_message, history):
84
+ return "", history + [[user_message, None]]
85
+
86
+ def bot(
87
+ history,
88
+ system_message,
89
+ max_tokens,
90
+ temperature,
91
+ top_p
92
+ ):
93
+ user_message = history[-1][0]
94
+ generator = respond(
95
+ user_message,
96
+ history[:-1],
97
+ system_message,
98
+ max_tokens,
99
+ temperature,
100
+ top_p
101
+ )
102
+
103
+ for response in generator:
104
+ if isinstance(response, tuple):
105
+ # This is the final response with audio
106
+ history[-1][1] = response[0]
107
+ yield history, response[1]
108
+ else:
109
+ # This is a text update
110
+ history[-1][1] = response
111
+ yield history, None
112
 
113
+ msg.submit(
114
+ user,
115
+ [msg, chatbot],
116
+ [msg, chatbot],
117
+ queue=False
118
+ ).then(
119
+ bot,
120
+ [chatbot, system_message, max_tokens, temperature, top_p],
121
+ [chatbot, audio_output]
122
+ )
123
 
124
  if __name__ == "__main__":
125
+ demo.launch()