Spaces:
Build error
Build error
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import nltk
|
| 3 |
+
import requests
|
| 4 |
+
import speech_recognition as sr
|
| 5 |
+
from rake_nltk import Rake
|
| 6 |
+
|
| 7 |
+
nltk.download('stopwords')
|
| 8 |
+
nltk.download('punkt')
|
| 9 |
+
|
| 10 |
+
# Replace with your actual API key and search engine ID
|
| 11 |
+
API_KEY = 'AIzaSyAjwvDJmEdbWhrzWR-17OctIS0ib4zfneU'
|
| 12 |
+
# Replace with your actual Custom Search Engine ID
|
| 13 |
+
SEARCH_ENGINE_KEY = '7564b8c73e277468a'
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
# Function to transcribe speech to text
|
| 17 |
+
def speech_to_text():
|
| 18 |
+
recognizer = sr.Recognizer()
|
| 19 |
+
with sr.Microphone() as source:
|
| 20 |
+
st.info("Listening... Speak into the microphone")
|
| 21 |
+
audio = recognizer.listen(source)
|
| 22 |
+
try:
|
| 23 |
+
text = recognizer.recognize_google(audio)
|
| 24 |
+
return text
|
| 25 |
+
except sr.UnknownValueError:
|
| 26 |
+
st.error("Speech not recognized")
|
| 27 |
+
except sr.RequestError as e:
|
| 28 |
+
st.error(f"Could not request results: {e}")
|
| 29 |
+
|
| 30 |
+
# Function to extract keywords using RAKE
|
| 31 |
+
def extract_keywords(text):
|
| 32 |
+
r = Rake()
|
| 33 |
+
r.extract_keywords_from_text(text)
|
| 34 |
+
keywords_with_scores = r.get_ranked_phrases_with_scores()
|
| 35 |
+
return [keyword for score, keyword in keywords_with_scores if score > 5]
|
| 36 |
+
|
| 37 |
+
# Streamlit app
|
| 38 |
+
st.set_page_config(layout="wide")
|
| 39 |
+
|
| 40 |
+
def main():
|
| 41 |
+
st.title("Speech to Text Image Search")
|
| 42 |
+
|
| 43 |
+
# Button to start speech to text conversion
|
| 44 |
+
if st.button("Start Speech to Text"):
|
| 45 |
+
transcribed_text = speech_to_text()
|
| 46 |
+
if transcribed_text:
|
| 47 |
+
st.info("Transcription complete:")
|
| 48 |
+
st.write(transcribed_text)
|
| 49 |
+
|
| 50 |
+
# Extract keywords from the transcribed text
|
| 51 |
+
st.subheader("Extracted Keywords")
|
| 52 |
+
keywords = extract_keywords(transcribed_text)
|
| 53 |
+
st.write(keywords)
|
| 54 |
+
|
| 55 |
+
# Use the transcribed text as the search query for image search
|
| 56 |
+
search_query = ' '.join(keywords)
|
| 57 |
+
url = "https://www.googleapis.com/customsearch/v1"
|
| 58 |
+
params = {
|
| 59 |
+
'q': search_query,
|
| 60 |
+
'key': API_KEY,
|
| 61 |
+
'cx': SEARCH_ENGINE_KEY,
|
| 62 |
+
'searchType': 'image'
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
response = requests.get(url, params=params)
|
| 66 |
+
results = response.json().get('items', [])
|
| 67 |
+
|
| 68 |
+
# Display the first two images side by side
|
| 69 |
+
st.subheader("Image Search Results")
|
| 70 |
+
if len(results) >= 2:
|
| 71 |
+
col1, col2 = st.columns(2)
|
| 72 |
+
col1.image(results[0]['link'], caption="Image 1", use_column_width=True)
|
| 73 |
+
col2.image(results[1]['link'], caption="Image 2", use_column_width=True)
|
| 74 |
+
else:
|
| 75 |
+
st.warning("Not enough images found for display.")
|
| 76 |
+
|
| 77 |
+
if __name__ == "__main__":
|
| 78 |
+
main()
|