|
|
|
|
|
import os |
|
import streamlit as st |
|
import pyttsx3 |
|
from openai import OpenAI |
|
|
|
|
|
api_key = os.getenv("OPENAI_API_KEY") |
|
|
|
|
|
if not api_key: |
|
st.error("OpenAI API key not found. Please set 'OPENAI_API_KEY' in environment variables.") |
|
st.stop() |
|
|
|
|
|
Model = "gpt-4o" |
|
client = OpenAI(api_key=api_key) |
|
|
|
|
|
engine = pyttsx3.init() |
|
voices = engine.getProperty('voices') |
|
engine.setProperty('voice', voices[0].id) |
|
|
|
def speak(text): |
|
engine.say(text) |
|
engine.runAndWait() |
|
|
|
|
|
def Reply(question): |
|
completion = client.chat.completions.create( |
|
model=Model, |
|
messages=[ |
|
{'role': "system", "content": "You are a helpful assistant"}, |
|
{'role': 'user', 'content': question} |
|
], |
|
max_tokens=200 |
|
) |
|
return completion.choices[0].message.content |
|
|
|
|
|
st.set_page_config(page_title="GPT Voice Assistant", page_icon="🗣️") |
|
st.title("🗣️ Voice Assistant using GPT-4o") |
|
st.write("Type your query below and get a spoken AI response!") |
|
|
|
user_query = st.text_input("Enter your query:") |
|
|
|
if st.button("Ask"): |
|
if user_query: |
|
with st.spinner("Thinking..."): |
|
response = Reply(user_query) |
|
st.success(response) |
|
speak(response) |
|
else: |
|
st.warning("Please enter a query first.") |
|
|