adinarayana commited on
Commit
5949d34
·
verified ·
1 Parent(s): c70f165

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -2
app.py CHANGED
@@ -2,6 +2,59 @@ from transformers import pipeline
2
  import gradio as gr
3
  from IPython.display import Audio
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  # Create a pipeline for text-to-speech
6
  tts = pipeline("text-to-speech", model="facebook/mms-tts-eng")
7
 
@@ -25,10 +78,11 @@ voice_reply = gr.Audio(type="filepath")
25
 
26
  iface = gr.Interface(
27
  fn=voice_chat,
28
- inputs=[gr.Audio(sources=["microphone"], type="filepath")],
29
  outputs=[gr.Textbox(label="ChatGPT Text") , gr.Audio(label = "ChatGPT Voice")],
30
  live=True,
31
  title="AI Voice Assistant with ChatGPT AI",
32
  )
33
 
34
- iface.launch(debug=True)
 
 
2
  import gradio as gr
3
  from IPython.display import Audio
4
 
5
+ # Create pipelines for text-to-speech and speech-to-text
6
+ tts = pipeline("text-to-speech", model="facebook/mms-tts-eng")
7
+ stt = pipeline("automatic-speech-recognition", model="openai/whisper-medium")
8
+
9
+ # Create pipeline for text generation, considering using a model trained for dialogue
10
+ chat = pipeline("text-generation", model="facebook/bart-base-conversational")
11
+
12
+ def handle_user_input(user_text, user_voice):
13
+ if user_text:
14
+ user_text = user_text.strip() # Remove leading/trailing whitespace
15
+ input_type = "text"
16
+ else:
17
+ try:
18
+ user_text = stt(user_voice)["text"]
19
+ except:
20
+ user_text = ""
21
+ input_type = "voice"
22
+
23
+ # Generate response
24
+ messages = [
25
+ {"role": "system", "content": "Hi! How can I help you today?"},
26
+ {"role": "user", "content": user_text},
27
+ ]
28
+ chat_reply = chat(messages=messages, max_length=100, top_p=0.95, temperature=0.7)[0]["generated_text"]
29
+ messages.append({"role": "assistant", "content": chat_reply})
30
+
31
+ # Generate audio output (only if input was text)
32
+ audio = None
33
+ if input_type == "text":
34
+ audio = tts(chat_reply)["audio"]
35
+
36
+ return chat_reply, audio
37
+
38
+ # Define input components
39
+ text_input = gr.Textbox(label="Enter your text (optional)")
40
+ voice_input = gr.Audio(sources=["microphone"], type="filepath")
41
+
42
+ # Create and launch the Gradio interface
43
+ iface = gr.Interface(
44
+ fn=handle_user_input,
45
+ inputs=[text_input, voice_input],
46
+ outputs=[gr.Textbox(label="Assistant Text"), gr.Audio(label="Assistant Voice (if text input)")],
47
+ live=True,
48
+ title="AI Voice Assistant",
49
+ )
50
+ iface.launch(debug=True)
51
+
52
+
53
+ """
54
+ from transformers import pipeline
55
+ import gradio as gr
56
+ from IPython.display import Audio
57
+
58
  # Create a pipeline for text-to-speech
59
  tts = pipeline("text-to-speech", model="facebook/mms-tts-eng")
60
 
 
78
 
79
  iface = gr.Interface(
80
  fn=voice_chat,
81
+ inputs=[gr.Textbox(label="Enter your text"), gr.Audio(sources=["microphone"], type="filepath")],
82
  outputs=[gr.Textbox(label="ChatGPT Text") , gr.Audio(label = "ChatGPT Voice")],
83
  live=True,
84
  title="AI Voice Assistant with ChatGPT AI",
85
  )
86
 
87
+ iface.launch(debug=True)
88
+ """