shresthasingh
commited on
Commit
•
6a14e1b
1
Parent(s):
1f303c9
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import json
|
3 |
+
from openai import OpenAI
|
4 |
+
from pathlib import Path
|
5 |
+
import tempfile
|
6 |
+
import os
|
7 |
+
|
8 |
+
# Initialize OpenAI client with hardcoded API key
|
9 |
+
# WARNING: This is not secure and should only be used for testing
|
10 |
+
client = os.getenv("openai")
|
11 |
+
|
12 |
+
# System prompt for GPT-4
|
13 |
+
SYSTEM_PROMPT = """
|
14 |
+
You are an AI assistant that converts user commands into structured JSON data. Your task is to interpret the user's intent and create a JSON object with relevant fields. Always include a 'response' field in the JSON with a friendly, conversational reply to the user's command.
|
15 |
+
|
16 |
+
For example:
|
17 |
+
User: "Play Taylor Swift on Spotify"
|
18 |
+
JSON: {
|
19 |
+
"action": "play music",
|
20 |
+
"application": "Spotify",
|
21 |
+
"artist": "Taylor Swift",
|
22 |
+
"response": "Sure, I'm playing Taylor Swift on Spotify for you now. Enjoy the music!"
|
23 |
+
}
|
24 |
+
|
25 |
+
User: "What's the weather like in New York?"
|
26 |
+
JSON: {
|
27 |
+
"action": "check weather",
|
28 |
+
"location": "New York",
|
29 |
+
"response": "I'm checking the weather in New York for you. One moment please."
|
30 |
+
}
|
31 |
+
|
32 |
+
Adapt the JSON structure to fit the context of the command, but always include the 'response' field.
|
33 |
+
"""
|
34 |
+
|
35 |
+
def process_command(command):
|
36 |
+
response = client.chat.completions.create(
|
37 |
+
model="gpt-4o",
|
38 |
+
messages=[
|
39 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
40 |
+
{"role": "user", "content": command}
|
41 |
+
]
|
42 |
+
)
|
43 |
+
return json.loads(response.choices[0].message.content)
|
44 |
+
|
45 |
+
def text_to_speech(text):
|
46 |
+
speech_file_path = Path(tempfile.gettempdir()) / "speech.mp3"
|
47 |
+
response = client.audio.speech.create(
|
48 |
+
model="tts-1",
|
49 |
+
voice="nova",
|
50 |
+
input=text
|
51 |
+
)
|
52 |
+
response.stream_to_file(speech_file_path)
|
53 |
+
return speech_file_path
|
54 |
+
|
55 |
+
st.title("Text Command Assistant")
|
56 |
+
|
57 |
+
# Text input for user command
|
58 |
+
command = st.text_input("Enter your command:")
|
59 |
+
|
60 |
+
if st.button("Process Command"):
|
61 |
+
if command:
|
62 |
+
# Process the text command
|
63 |
+
with st.spinner("Processing command..."):
|
64 |
+
result = process_command(command)
|
65 |
+
|
66 |
+
# Display JSON result
|
67 |
+
st.json(result)
|
68 |
+
|
69 |
+
# Extract response and convert to speech
|
70 |
+
response_text = result["response"]
|
71 |
+
with st.spinner("Generating speech..."):
|
72 |
+
speech_file = text_to_speech(response_text)
|
73 |
+
|
74 |
+
# Play the generated speech
|
75 |
+
st.audio(str(speech_file))
|
76 |
+
else:
|
77 |
+
st.warning("Please enter a command.")
|
78 |
+
|
79 |
+
st.markdown("---")
|
80 |
+
st.write("Created by Shrestha Singh")
|
81 |
+
|