SahilCarterr commited on
Commit
1ea52b6
·
verified ·
1 Parent(s): 5322403

Upload 3 files

Browse files
Files changed (3) hide show
  1. README.md +4 -4
  2. app.py +121 -0
  3. requirements.txt +1 -0
README.md CHANGED
@@ -1,10 +1,10 @@
1
  ---
2
- title: Voice Chat
3
- emoji: 📊
4
- colorFrom: gray
5
  colorTo: pink
6
  sdk: gradio
7
- sdk_version: 5.23.1
8
  app_file: app.py
9
  pinned: false
10
  ---
 
1
  ---
2
+ title: Avia Demo
3
+ emoji: 🦀
4
+ colorFrom: red
5
  colorTo: pink
6
  sdk: gradio
7
+ sdk_version: 5.15.0
8
  app_file: app.py
9
  pinned: false
10
  ---
app.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import openai
3
+ import os
4
+ import tempfile
5
+
6
+ openai.api_key = os.getenv("OPENAI_API_KEY")
7
+
8
+ def transcribe_audio(audio_file):
9
+ with open(audio_file, "rb") as f:
10
+ transcript = openai.audio.transcriptions.create(
11
+ model="whisper-1",
12
+ file=f
13
+ )
14
+ return transcript.text
15
+
16
+ def generate_response(conversation_history):
17
+ response = openai.chat.completions.create(
18
+ model="ft:gpt-4o-2024-08-06:personal::BA52Cq4i",
19
+ messages=conversation_history
20
+ )
21
+ reply = response.choices[0].message.content
22
+ return reply
23
+
24
+ def text_to_speech(text):
25
+ response = openai.audio.speech.create(
26
+ model="tts-1",
27
+ voice="alloy",
28
+ input=text,
29
+ )
30
+ temp_audio_path = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False).name
31
+ response.stream_to_file(temp_audio_path)
32
+ return temp_audio_path
33
+
34
+ def process_input(audio, text, conversation_history):
35
+ if not conversation_history:
36
+ conversation_history = [{"role": "system", "content": "You are a pilot assistant."}]
37
+ transcribed_text = ""
38
+ if audio:
39
+ transcribed_text = transcribe_audio(audio)
40
+ else:
41
+ transcribed_text = text or ""
42
+ if not transcribed_text.strip():
43
+ return (
44
+ "No text found. Please provide audio or type text.",
45
+ "Please provide text or record audio.",
46
+ None,
47
+ conversation_history
48
+ )
49
+ conversation_history.append({"role": "user", "content": transcribed_text})
50
+ generated_text = generate_response(conversation_history)
51
+ conversation_history.append({"role": "assistant", "content": generated_text})
52
+ audio_output = None
53
+ try:
54
+ audio_output = text_to_speech(generated_text)
55
+ except Exception:
56
+ pass
57
+ return transcribed_text, generated_text, audio_output, conversation_history
58
+
59
+ def update_chat_history(conversation_history):
60
+ return "\n".join(
61
+ f"{msg['role'].capitalize()}: {msg['content']}" for msg in conversation_history
62
+ )
63
+
64
+ with gr.Blocks() as demo:
65
+ gr.Markdown("# OpenAI Multi-turn Voice & Text Chat")
66
+ user_name = gr.Textbox(label="Enter your name")
67
+
68
+ with gr.Row():
69
+ audio_input = gr.Audio(
70
+ sources="microphone",
71
+ type="filepath",
72
+ label="Record Audio"
73
+ )
74
+ text_input = gr.Textbox(
75
+ label="Or Type Text",
76
+ placeholder="Enter text here..."
77
+ )
78
+
79
+ generate_btn = gr.Button("Generate Response")
80
+ conversation_history = gr.State([])
81
+
82
+ transcribed_textbox = gr.Textbox(
83
+ label="Transcribed Speech-to-Text",
84
+ interactive=False,
85
+ lines=1
86
+ )
87
+ output_text = gr.Textbox(
88
+ label="GPT Response",
89
+ interactive=False,
90
+ lines=2
91
+ )
92
+ output_audio = gr.Audio(
93
+ label="Generated Audio",
94
+ autoplay=True,
95
+ interactive=False
96
+ )
97
+ chat_history_display = gr.Textbox(
98
+ label="Conversation History",
99
+ interactive=False,
100
+ lines=3
101
+ )
102
+
103
+ generate_btn.click(
104
+ fn=process_input,
105
+ inputs=[audio_input, text_input, conversation_history],
106
+ outputs=[transcribed_textbox, output_text, output_audio, conversation_history]
107
+ ).then(
108
+ fn=update_chat_history,
109
+ inputs=[conversation_history],
110
+ outputs=[chat_history_display]
111
+ )
112
+
113
+ end_conversation = gr.Button("End Conversation")
114
+ end_conversation.click(
115
+ fn=None,
116
+ inputs=[],
117
+ outputs=[],
118
+ js="() => { window.location.reload() }"
119
+ )
120
+
121
+ demo.launch(share=True)
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ openai