lol040604lol commited on
Commit
eb4ebd0
·
verified ·
1 Parent(s): 6103fd7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -120
app.py CHANGED
@@ -248,135 +248,58 @@ if st.button("Stopo Listening"):
248
  st.write(f"- {recommendation}")
249
 
250
  # HTML/JavaScript for Audio Recording in the Browser
251
- html_code = """
252
- <script>
253
- let mediaRecorder;
254
- let audioChunks = [];
255
-
256
- async function startRecording() {
257
- const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
258
- mediaRecorder = new MediaRecorder(stream);
259
-
260
- mediaRecorder.ondataavailable = (event) => {
261
- audioChunks.push(event.data);
262
- };
263
-
264
- mediaRecorder.onstop = () => {
265
- const audioBlob = new Blob(audioChunks, { type: "audio/webm" });
266
- audioChunks = [];
267
- sendAudioToBackend(audioBlob);
268
- };
269
-
270
- mediaRecorder.start(100); // Record audio in small chunks (100ms)
271
- }
272
-
273
- function sendAudioToBackend(audioBlob) {
274
- const formData = new FormData();
275
- formData.append("audio", audioBlob);
276
- fetch("/process_audio", {
277
- method: "POST",
278
- body: formData,
279
- })
280
- .then((response) => response.json())
281
- .then((data) => {
282
- console.log("Transcription:", data.transcription);
283
- document.getElementById("transcription").innerText = data.transcription;
284
- })
285
- .catch((error) => console.error("Error:", error));
286
- }
287
-
288
- function stopRecording() {
289
- mediaRecorder.stop();
290
- }
291
- </script>
292
-
293
- <button onclick="startRecording()">Start Recording</button>
294
- <button onclick="stopRecording()">Stop Recording</button>
295
- <p id="transcription"></p>
296
- """
297
- recognizer = sr.Recognizer()
298
- microphone = sr.Microphone()
299
-
300
- # Display the HTML interface for recording
301
- st.markdown(html_code, unsafe_allow_html=True)
302
-
303
- def process_audio(audio_file):
304
- # Convert the audio to WAV if necessary, and use speech recognition
305
- recognizer = sr.Recognizer()
306
- audio_path = "temp_audio.webm"
307
- with open(audio_path, "wb") as f:
308
- f.write(audio_file.getbuffer())
309
 
310
- # Transcription with SpeechRecognition library (Google API)
311
- with sr.AudioFile(audio_path) as source:
312
- audio_data = recognizer.record(source)
313
- transcription = recognizer.recognize_google(audio_data)
314
-
315
- return transcription
316
 
317
- # Streamlit Server to handle POST requests (this will work in FastAPI or directly in your Streamlit app)
318
- import os
319
- from fastapi import FastAPI, File, UploadFile
320
- from starlette.responses import JSONResponse
321
 
322
- app = FastAPI()
 
 
 
 
323
 
324
- @app.post("/process_audio")
325
- async def process_audio_endpoint(file: UploadFile = File(...)):
326
- transcription = process_audio(file)
327
- return JSONResponse({"transcription": transcription})
328
 
329
- def transcribe_audio():
330
- with microphone as source:
331
- recognizer.adjust_for_ambient_noise(source) # Adjust for ambient noise
332
- audio = recognizer.listen(source) # Listen to the source (microphone)
 
 
 
 
 
 
 
 
 
 
 
333
  try:
334
- # Recognize speech using Google Web Speech API or other recognizers
335
- transcription = recognizer.recognize_google(audio)
336
  return transcription
337
  except sr.UnknownValueError:
338
- # Could not understand the speech
339
  return "Could not understand audio."
340
  except sr.RequestError as e:
341
- # API error
342
  return f"Error with the speech recognition service: {e}"
343
 
344
- # Add some simple session management
345
- session_data = {"interactions": [], "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")}
346
- if st.button("Startass Listening"):
347
- st.write("Listening... Speak into the microphone.")
348
-
349
- # Process real-time audio and show results
350
- while True:
351
- transcription = transcribe_audio()
352
- if transcription:
353
- st.write(f"User: {transcription}")
354
-
355
- # Product Recommendations
356
- recommendations = recommend_products(transcription)
357
- st.subheader("Product Recommendations")
358
- for title, description in recommendations:
359
- st.write(f"- **{title}**: {description}")
360
-
361
- # Objection Handling
362
- objection, response = handle_objection(transcription)
363
- st.subheader("Objection Handling")
364
- st.write(f"**Objection:** {objection}")
365
- st.write(f"**Response:** {response}")
366
-
367
- # Sentiment Analysis
368
- sentiment = analyze_sentiment(transcription)
369
- st.subheader("Sentiment Analysis")
370
- st.write(f"**Sentiment:** {sentiment['label']}")
371
- st.write(f"**Score:** {sentiment['score']}")
372
-
373
- # Add interaction to session data and update JSON file
374
- interaction = {
375
- "transcription": transcription,
376
- "sentiment": sentiment,
377
- "product_recommendations": recommendations,
378
- "objection_handling": {"objection": objection, "response": response},
379
- }
380
- session_data["interactions"].append(interaction)
381
- with open("session_data.json", "w") as f:
382
- json.dump(session_data, f, indent=4)
 
248
  st.write(f"- {recommendation}")
249
 
250
  # HTML/JavaScript for Audio Recording in the Browser
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
251
 
252
+ import gradio as gr
253
+ import speech_recognition as sr
254
+ from datetime import datetime
 
 
 
255
 
256
+ # Initialize recognizer
257
+ recognizer = sr.Recognizer()
 
 
258
 
259
+ # Add some simple session management
260
+ import gradio as gr
261
+ import speech_recognition as sr
262
+ import json
263
+ from datetime import datetime
264
 
265
+ # Initialize recognizer and microphone
266
+ recognizer = sr.Recognizer()
 
 
267
 
268
+ # Sample product recommendation function (replace with your own logic)
269
+ def recommend_products(transcription):
270
+ products = {
271
+ "phone": [("Smartphone", "Latest model with 5G support"), ("Phone Case", "Durable and stylish")],
272
+ "laptop": [("Laptop", "High performance for gaming and work"), ("Laptop Stand", "Ergonomic and adjustable")],
273
+ }
274
+ recommendations = []
275
+ for keyword, product_list in products.items():
276
+ if keyword.lower() in transcription.lower():
277
+ recommendations.extend(product_list)
278
+ return recommendations
279
+
280
+ def transcribe_audio(audio):
281
+ """Transcribe audio captured from Gradio."""
282
+ audio_data = sr.AudioData(audio, 16000, 2)
283
  try:
284
+ transcription = recognizer.recognize_google(audio_data)
 
285
  return transcription
286
  except sr.UnknownValueError:
 
287
  return "Could not understand audio."
288
  except sr.RequestError as e:
 
289
  return f"Error with the speech recognition service: {e}"
290
 
291
+ def gradio_interface(audio):
292
+ """Gradio interface for capturing audio."""
293
+ transcription = transcribe_audio(audio)
294
+
295
+ # Product recommendations based on transcription
296
+ recommendations = recommend_products(transcription)
297
+
298
+ recommendation_text = ""
299
+ for title, description in recommendations:
300
+ recommendation_text += f"- **{title}**: {description}\n"
301
+
302
+ return transcription, recommendation_text
303
+
304
+ # Gradio UI for capturing audio and displaying transcription + recommendations
305
+ gr.Interface(fn=gradio_interface, inputs="microphone", outputs=["text", "text"]).launch(share=True)