Joe7oo7 commited on
Commit
5828241
·
verified ·
1 Parent(s): e83a276

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -54
app.py CHANGED
@@ -1,7 +1,5 @@
1
- import gradio as gr
2
- import speech_recognition as sr
3
  from huggingface_hub import InferenceClient
4
- from io import BytesIO
5
 
6
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
7
 
@@ -13,44 +11,29 @@ def respond(
13
  temperature,
14
  top_p,
15
  ):
16
- try:
17
- messages = [{"role": "system", "content": system_message}]
18
 
19
- for val in history:
20
- if val[0]:
21
- messages.append({"role": "user", "content": val[0]})
22
- if val[1]:
23
- messages.append({"role": "assistant", "content": val[1]})
24
 
25
- messages.append({"role": "user", "content": message})
26
 
27
- response = ""
28
 
29
- for message in client.chat_completion(
30
- messages,
31
- max_tokens=max_tokens,
32
- stream=True,
33
- temperature=temperature,
34
- top_p=top_p,
35
- ):
36
- token = message.choices[0].delta.content
37
- response += token
38
- yield response
39
-
40
- except Exception as e:
41
- yield f"An error occurred: {str(e)}"
42
 
43
- def recognize_speech(audio_file):
44
- recognizer = sr.Recognizer()
45
- with sr.AudioFile(audio_file) as source:
46
- audio_data = recognizer.record(source)
47
- try:
48
- text = recognizer.recognize_google(audio_data)
49
- return text
50
- except sr.UnknownValueError:
51
- return "Sorry, I could not understand the audio."
52
- except sr.RequestError as e:
53
- return f"Could not request results; {e}"
54
 
55
  # Define custom CSS
56
  custom_css = """
@@ -87,23 +70,22 @@ body {
87
  """
88
 
89
  # Create a Gradio chat interface with custom CSS
90
- with gr.Blocks(css=custom_css) as demo:
91
- system_msg = gr.Textbox(value="You are a Chatbot. Your name is Evy. You are Developed By Joe.", label="System message")
92
- max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens")
93
- temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
94
- top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")
95
-
96
- input_textbox = gr.Textbox(label="Your message")
97
- voice_input = gr.Audio(source="microphone", type="file", label="Record your message")
98
- with gr.Row():
99
- submit_button = gr.Button("Send")
100
- voice_button = gr.Button("🎤 Speak")
101
-
102
- chatbot_output = gr.Textbox(label="Chatbot response", interactive=False)
 
 
 
103
 
104
- submit_button.click(respond, inputs=[input_textbox, gr.State([]), system_msg, max_tokens, temperature, top_p], outputs=chatbot_output)
105
- voice_input.change(lambda x: recognize_speech(BytesIO(x)), inputs=[voice_input], outputs=input_textbox)
106
-
107
- # Launch the interface
108
  if __name__ == "__main__":
109
- demo.launch()
 
1
+ mport gradio as gr
 
2
  from huggingface_hub import InferenceClient
 
3
 
4
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
5
 
 
11
  temperature,
12
  top_p,
13
  ):
14
+ messages = [{"role": "system", "content": system_message}]
 
15
 
16
+ for val in history:
17
+ if val[0]:
18
+ messages.append({"role": "user", "content": val[0]})
19
+ if val[1]:
20
+ messages.append({"role": "assistant", "content": val[1]})
21
 
22
+ messages.append({"role": "user", "content": message})
23
 
24
+ response = ""
25
 
26
+ for message in client.chat_completion(
27
+ messages,
28
+ max_tokens=max_tokens,
29
+ stream=True,
30
+ temperature=temperature,
31
+ top_p=top_p,
32
+ ):
33
+ token = message.choices[0].delta.content
 
 
 
 
 
34
 
35
+ response += token
36
+ yield response
 
 
 
 
 
 
 
 
 
37
 
38
  # Define custom CSS
39
  custom_css = """
 
70
  """
71
 
72
  # Create a Gradio chat interface with custom CSS
73
+ demo = gr.ChatInterface(
74
+ fn=respond,
75
+ additional_inputs=[
76
+ gr.Textbox(value="You are a Chatbot.Your name is Evy.Your are Developed By Joe.", label="System message"),
77
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
78
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
79
+ gr.Slider(
80
+ minimum=0.1,
81
+ maximum=1.0,
82
+ value=0.95,
83
+ step=0.05,
84
+ label="Top-p (nucleus sampling)",
85
+ ),
86
+ ],
87
+ css=custom_css
88
+ )
89
 
 
 
 
 
90
  if __name__ == "__main__":
91
+ demo.launch()