JaweriaGenAI commited on
Commit
0815af0
Β·
verified Β·
1 Parent(s): 2ddc050

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -30
app.py CHANGED
@@ -4,18 +4,12 @@ import json, re
4
  import openai
5
  from datetime import datetime
6
  from pydub import AudioSegment
7
- import tempfile
8
- import sounddevice as sd
9
- from scipy.io.wavfile import write
10
  import pdfplumber, docx, pandas as pd
11
  from PIL import Image
12
 
13
  openai.api_key = os.environ.get("GROQ_API_KEY")
14
  openai.api_base = "https://api.groq.com/openai/v1"
15
 
16
- recording = False
17
- recorded_file = None
18
-
19
  # Chat with Groq
20
  def chat_with_groq(message, state):
21
  if state is None:
@@ -76,26 +70,6 @@ def load_chat(name):
76
  except Exception as e:
77
  return [], {"oai_history": [], "chatbot_ui": []}, f"❌ Could not load {name}: {e}"
78
 
79
- # Voice record start/stop
80
- def toggle_record():
81
- global recording, recorded_file
82
- if not recording:
83
- recording = True
84
- return gr.update(value="πŸŽ™οΈ Recording..."), gr.update(visible=False)
85
- else:
86
- recording = False
87
- fs = 44100
88
- duration = 5
89
- tmp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
90
- try:
91
- audio = sd.rec(int(duration * fs), samplerate=fs, channels=1)
92
- sd.wait()
93
- write(tmp_file.name, fs, audio)
94
- recorded_file = tmp_file.name
95
- return transcribe_audio(recorded_file), gr.update(visible=True)
96
- except Exception as e:
97
- return f"❌ Recording failed: {e}", gr.update(visible=True)
98
-
99
  # Transcription
100
  def transcribe_audio(file):
101
  if not file: return ""
@@ -157,7 +131,10 @@ textarea, input[type='text'] { background: #f0f0f0; border-radius: 8px; }
157
  with gr.Row():
158
  chat_input = gr.Textbox(placeholder="Type here or use mic...", scale=6, show_label=False)
159
  send_btn = gr.Button("πŸš€", scale=1)
160
- mic_btn = gr.Button("πŸŽ™οΈ", scale=1)
 
 
 
161
 
162
  with gr.Row():
163
  file_upload = gr.File(label="πŸ“Ž Upload file", file_types=[".pdf", ".docx", ".txt", ".csv", ".xlsx", ".jpg", ".png"])
@@ -174,8 +151,6 @@ textarea, input[type='text'] { background: #f0f0f0; border-radius: 8px; }
174
  send_btn.click(chat_with_groq, [chat_input, state], [chat_input, chatbot, state])
175
  chat_input.submit(chat_with_groq, [chat_input, state], [chat_input, chatbot, state])
176
 
177
- mic_btn.click(toggle_record, [], [chat_input, mic_btn])
178
-
179
  process_btn.click(process_file, [file_upload], [chat_input])
180
 
181
  new_btn.click(lambda: ("", [], {"oai_history": [], "chatbot_ui": []}), [], [chat_input, chatbot, state])
@@ -183,4 +158,4 @@ textarea, input[type='text'] { background: #f0f0f0; border-radius: 8px; }
183
  save_btn.click(lambda: gr.update(choices=list_saved_files()), [], [dropdown])
184
  load_btn.click(load_chat, [dropdown], [chatbot, state, status])
185
 
186
- demo.launch()
 
4
  import openai
5
  from datetime import datetime
6
  from pydub import AudioSegment
 
 
 
7
  import pdfplumber, docx, pandas as pd
8
  from PIL import Image
9
 
10
  openai.api_key = os.environ.get("GROQ_API_KEY")
11
  openai.api_base = "https://api.groq.com/openai/v1"
12
 
 
 
 
13
  # Chat with Groq
14
  def chat_with_groq(message, state):
15
  if state is None:
 
70
  except Exception as e:
71
  return [], {"oai_history": [], "chatbot_ui": []}, f"❌ Could not load {name}: {e}"
72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  # Transcription
74
  def transcribe_audio(file):
75
  if not file: return ""
 
131
  with gr.Row():
132
  chat_input = gr.Textbox(placeholder="Type here or use mic...", scale=6, show_label=False)
133
  send_btn = gr.Button("πŸš€", scale=1)
134
+
135
+ with gr.Row():
136
+ mic_audio = gr.Audio(source="microphone", type="filepath", label="πŸŽ™οΈ Record Voice")
137
+ mic_audio.change(transcribe_audio, [mic_audio], [chat_input])
138
 
139
  with gr.Row():
140
  file_upload = gr.File(label="πŸ“Ž Upload file", file_types=[".pdf", ".docx", ".txt", ".csv", ".xlsx", ".jpg", ".png"])
 
151
  send_btn.click(chat_with_groq, [chat_input, state], [chat_input, chatbot, state])
152
  chat_input.submit(chat_with_groq, [chat_input, state], [chat_input, chatbot, state])
153
 
 
 
154
  process_btn.click(process_file, [file_upload], [chat_input])
155
 
156
  new_btn.click(lambda: ("", [], {"oai_history": [], "chatbot_ui": []}), [], [chat_input, chatbot, state])
 
158
  save_btn.click(lambda: gr.update(choices=list_saved_files()), [], [dropdown])
159
  load_btn.click(load_chat, [dropdown], [chatbot, state, status])
160
 
161
+ demo.launch()