Ayesha931 commited on
Commit
e9ec531
·
verified ·
1 Parent(s): 72ebc89

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -52
app.py CHANGED
@@ -7,41 +7,16 @@ 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):
13
- recognizer = sr.Recognizer()
14
- with sr.AudioFile(audio_file) as source:
15
- audio = recognizer.record(source)
16
- try:
17
- text = recognizer.recognize_google(audio)
18
- return text
19
- except sr.UnknownValueError:
20
- return "Sorry, I did not understand the audio"
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 = ""
27
- pdf_document = fitz.open(pdf_file)
28
- for page_num in range(len(pdf_document)):
29
- page = pdf_document.load_page(page_num)
30
- text += page.get_text()
31
- return text
32
-
33
- # Function to embed text using a transformer model
34
- def embed_text(texts, model, tokenizer):
35
- inputs = tokenizer(texts, return_tensors='pt', truncation=True, padding=True)
36
- with torch.no_grad():
37
- embeddings = model(**inputs).last_hidden_state.mean(dim=1).numpy()
38
- return embeddings
39
 
40
- # Function to convert text to speech
41
- def text_to_speech(text, output_file):
42
- tts = gTTS(text=text, lang='en')
43
- tts.save(output_file)
44
- return output_file
45
 
46
  # Initialize model and tokenizer
47
  tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
@@ -52,7 +27,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')]
@@ -66,26 +41,28 @@ for path in pdf_paths:
66
  embeddings = embed_text(texts, model, tokenizer)
67
  index.add(embeddings)
68
 
69
- # Streamlit application
70
- st.title("Parenting Guide App")
 
 
 
 
 
 
 
71
 
72
- audio_file = st.file_uploader("Upload an audio file", type=["wav", "mp3"])
 
 
 
73
 
74
- if audio_file:
75
- st.write("Processing...")
 
76
 
77
- # Convert audio to text
78
- text = audio_to_text(audio_file)
79
- st.write("Voice command:", text)
80
 
81
- # Find relevant advice
82
- query_embedding = embed_text([text], model, tokenizer)
83
- D, I = index.search(query_embedding, k=1) # Search for the most similar advice
84
- closest_text = texts[I[0][0]]
85
-
86
- st.write("Advice:", closest_text)
87
 
88
- # Convert advice to speech
89
- output_file = "advice.mp3"
90
- output_path = text_to_speech(closest_text, output_file)
91
- st.audio(output_path)
 
7
  import faiss
8
  import numpy as np
9
  from gtts import gTTS
10
+ import io
11
+ from PIL import Image
12
+ from flask import Flask, request, jsonify
13
+ from werkzeug.utils import secure_filename
14
 
15
+ # Initialize Streamlit components
16
+ st.title("Parenting Guide App")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
+ # HTML component for the recording interface
19
+ st.components.v1.html(open("audio_recorder.html").read(), height=600)
 
 
 
20
 
21
  # Initialize model and tokenizer
22
  tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
 
27
  index = faiss.IndexFlatL2(dimension)
28
 
29
  # Folder path containing PDFs
30
+ pdf_folder_path = "pdfs"
31
 
32
  # Read all PDF files from the specified folder
33
  pdf_paths = [os.path.join(pdf_folder_path, f) for f in os.listdir(pdf_folder_path) if f.endswith('.pdf')]
 
41
  embeddings = embed_text(texts, model, tokenizer)
42
  index.add(embeddings)
43
 
44
+ # Define Flask app to handle audio uploads
45
+ app = Flask(__name__)
46
+
47
+ @app.route('/upload-audio', methods=['POST'])
48
+ def upload_audio():
49
+ audio_file = request.files['audio']
50
+ if audio_file:
51
+ audio_file.save('temp_audio.wav')
52
+ text = audio_to_text('temp_audio.wav')
53
 
54
+ # Find relevant advice
55
+ query_embedding = embed_text([text], model, tokenizer)
56
+ D, I = index.search(query_embedding, k=1) # Search for the most similar advice
57
+ closest_text = texts[I[0][0]]
58
 
59
+ # Convert advice to speech
60
+ output_file = "advice.mp3"
61
+ output_path = text_to_speech(closest_text, output_file)
62
 
63
+ return jsonify({'status': 'success', 'audio': output_file})
 
 
64
 
65
+ return jsonify({'status': 'error', 'message': 'No audio file received'})
 
 
 
 
 
66
 
67
+ if __name__ == '__main__':
68
+ app.run(debug=True)