Update app.py
Browse files
app.py
CHANGED
@@ -7,16 +7,41 @@ import torch
|
|
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 |
-
#
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
-
#
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
# Initialize model and tokenizer
|
22 |
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
|
@@ -41,28 +66,31 @@ for path in pdf_paths:
|
|
41 |
embeddings = embed_text(texts, model, tokenizer)
|
42 |
index.add(embeddings)
|
43 |
|
44 |
-
#
|
45 |
-
|
46 |
|
47 |
-
|
48 |
-
|
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 |
-
|
55 |
-
|
56 |
-
D, I = index.search(query_embedding, k=1) # Search for the most similar advice
|
57 |
-
closest_text = texts[I[0][0]]
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
|
63 |
-
|
|
|
|
|
64 |
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
-
|
68 |
-
|
|
|
|
|
|
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")
|
|
|
66 |
embeddings = embed_text(texts, model, tokenizer)
|
67 |
index.add(embeddings)
|
68 |
|
69 |
+
# Streamlit application
|
70 |
+
st.title("Parenting Guide App")
|
71 |
|
72 |
+
# Upload an audio file
|
73 |
+
audio_file = st.file_uploader("Record and upload your audio file (WAV/MP3)", type=["wav", "mp3"])
|
|
|
|
|
|
|
|
|
74 |
|
75 |
+
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
|
87 |
+
query_embedding = embed_text([text], model, tokenizer)
|
88 |
+
D, I = index.search(query_embedding, k=1) # Search for the most similar advice
|
89 |
+
closest_text = texts[I[0][0]]
|
90 |
+
|
91 |
+
st.write("Advice:", closest_text)
|
92 |
|
93 |
+
# Convert advice to speech
|
94 |
+
output_file = "advice.mp3"
|
95 |
+
output_path = text_to_speech(closest_text, output_file)
|
96 |
+
st.audio(output_path)
|