JaganathC commited on
Commit
14d23d5
·
verified ·
1 Parent(s): cf46ef2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -0
app.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+
3
+ import os
4
+ import streamlit as st
5
+ import pyttsx3
6
+ from openai import OpenAI
7
+
8
+ # Load API key from environment variable
9
+ api_key = os.getenv("OPENAI_API_KEY")
10
+
11
+ # Check if API key exists
12
+ if not api_key:
13
+ st.error("OpenAI API key not found. Please set 'OPENAI_API_KEY' in environment variables.")
14
+ st.stop()
15
+
16
+ # OpenAI client setup
17
+ Model = "gpt-4o"
18
+ client = OpenAI(api_key=api_key)
19
+
20
+ # Text-to-speech setup
21
+ engine = pyttsx3.init()
22
+ voices = engine.getProperty('voices')
23
+ engine.setProperty('voice', voices[0].id)
24
+
25
+ def speak(text):
26
+ engine.say(text)
27
+ engine.runAndWait()
28
+
29
+ # GPT response function
30
+ def Reply(question):
31
+ completion = client.chat.completions.create(
32
+ model=Model,
33
+ messages=[
34
+ {'role': "system", "content": "You are a helpful assistant"},
35
+ {'role': 'user', 'content': question}
36
+ ],
37
+ max_tokens=200
38
+ )
39
+ return completion.choices[0].message.content
40
+
41
+ # Streamlit UI
42
+ st.set_page_config(page_title="GPT Voice Assistant", page_icon="🗣️")
43
+ st.title("🗣️ Voice Assistant using GPT-4o")
44
+ st.write("Type your query below and get a spoken AI response!")
45
+
46
+ user_query = st.text_input("Enter your query:")
47
+
48
+ if st.button("Ask"):
49
+ if user_query:
50
+ with st.spinner("Thinking..."):
51
+ response = Reply(user_query)
52
+ st.success(response)
53
+ speak(response)
54
+ else:
55
+ st.warning("Please enter a query first.")