Ayesha931 commited on
Commit
ae08f46
·
verified ·
1 Parent(s): eab2b1b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -3
app.py CHANGED
@@ -7,6 +7,7 @@ import torch
7
  import faiss
8
  import numpy as np
9
  from gtts import gTTS
 
10
 
11
  # Function to convert audio file to text
12
  def audio_to_text(audio_file):
@@ -21,6 +22,13 @@ def audio_to_text(audio_file):
21
  except sr.RequestError:
22
  return "Sorry, there was a problem with the request"
23
 
 
 
 
 
 
 
 
24
  # Function to extract text from a PDF file
25
  def extract_text_from_pdf(pdf_file):
26
  text = ""
@@ -52,7 +60,7 @@ dimension = 768 # Size of BERT embeddings
52
  index = faiss.IndexFlatL2(dimension)
53
 
54
  # Folder path containing PDFs
55
- pdf_folder_path = "pdfsforRAG"
56
 
57
  # Read all PDF files from the specified folder
58
  pdf_paths = [os.path.join(pdf_folder_path, f) for f in os.listdir(pdf_folder_path) if f.endswith('.pdf')]
@@ -76,11 +84,14 @@ if audio_file:
76
  st.write("Processing...")
77
 
78
  # Save the uploaded audio file
79
- with open("temp_audio.wav", "wb") as f:
80
  f.write(audio_file.getbuffer())
81
 
 
 
 
82
  # Convert audio to text
83
- text = audio_to_text("temp_audio.wav")
84
  st.write("Voice command:", text)
85
 
86
  # Find relevant advice
 
7
  import faiss
8
  import numpy as np
9
  from gtts import gTTS
10
+ from pydub import AudioSegment
11
 
12
  # Function to convert audio file to text
13
  def audio_to_text(audio_file):
 
22
  except sr.RequestError:
23
  return "Sorry, there was a problem with the request"
24
 
25
+ # Function to convert audio to WAV format
26
+ def convert_to_wav(audio_file_path):
27
+ audio = AudioSegment.from_file(audio_file_path)
28
+ wav_path = "temp_audio.wav"
29
+ audio.export(wav_path, format="wav")
30
+ return wav_path
31
+
32
  # Function to extract text from a PDF file
33
  def extract_text_from_pdf(pdf_file):
34
  text = ""
 
60
  index = faiss.IndexFlatL2(dimension)
61
 
62
  # Folder path containing PDFs
63
+ pdf_folder_path = "pdfs"
64
 
65
  # Read all PDF files from the specified folder
66
  pdf_paths = [os.path.join(pdf_folder_path, f) for f in os.listdir(pdf_folder_path) if f.endswith('.pdf')]
 
84
  st.write("Processing...")
85
 
86
  # Save the uploaded audio file
87
+ with open("temp_audio.mp3", "wb") as f:
88
  f.write(audio_file.getbuffer())
89
 
90
+ # Convert audio to WAV format if needed
91
+ wav_path = convert_to_wav("temp_audio.mp3")
92
+
93
  # Convert audio to text
94
+ text = audio_to_text(wav_path)
95
  st.write("Voice command:", text)
96
 
97
  # Find relevant advice