chatbot_voice / app.py
raomyousaf's picture
Update app.py
63cba62 verified
Raw
History Blame Contribute Delete
2.54 kB
import os
import streamlit as st
import speech_recognition as sr
from groq import Groq
from datetime import datetime
import requests
# Groq API setup
client = Groq(api_key=("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"))
# Streamlit UI setup
st.title("Voice-Activated Personal Assistant")
# Function to greet user
def greet_user():
st.write("Hello, how can I assist you today?")
# Function to get real-time weather info
def get_weather():
api_key = "YOUR_WEATHER_API_KEY" # Replace with your weather API key
location = "London" # Example location
url = f"http://api.openweathermap.org/data/2.5/weather?q={location}&appid={api_key}"
response = requests.get(url).json()
if response['cod'] == 200:
main_data = response['main']
weather_data = response['weather'][0]
return f"Weather in {location}: {weather_data['description']}, Temperature: {main_data['temp']}K"
else:
return "Could not fetch weather information."
# Function for speech recognition
def recognize_speech():
recognizer = sr.Recognizer()
with sr.Microphone() as source:
st.write("Listening...")
audio = recognizer.listen(source)
try:
command = recognizer.recognize_google(audio)
st.write(f"You said: {command}")
return command
except sr.UnknownValueError:
st.write("Sorry, I did not understand that.")
return ""
except sr.RequestError:
st.write("Could not request results; check your internet connection.")
return ""
# Function to get a response from Groq
def get_groq_response(user_input):
chat_completion = client.chat.completions.create(
messages=[{"role": "user", "content": user_input}],
model="llama-3.3-70b-versatile",
stream=False,
)
return chat_completion.choices[0].message.content
# Display greeting
greet_user()
# Text input or speech recognition
user_input = st.text_input("Type your query here:")
if st.button("Use Voice Command"):
user_input = recognize_speech()
# Get response from Groq
if user_input:
response = get_groq_response(user_input)
st.write("Response from AI Model:")
st.write(response)
# Real-time information like weather or time
if st.button("Get Weather Information"):
weather_info = get_weather()
st.write(weather_info)
if st.button("Get Current Time"):
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
st.write(f"Current Time: {current_time}")