shresthasingh's picture
Update app.py
7469aa6 verified
raw
history blame
2.47 kB
import streamlit as st
import json
from openai import OpenAI
from pathlib import Path
import tempfile
import os
# Initialize OpenAI client with hardcoded API key
# WARNING: This is not secure and should only be used for testing
client = os.getenv("openai")
# System prompt for GPT-4
SYSTEM_PROMPT = """
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.
For example:
User: "Play Taylor Swift on Spotify"
JSON: {
"action": "play music",
"application": "Spotify",
"artist": "Taylor Swift",
"response": "Sure, I'm playing Taylor Swift on Spotify for you now. Enjoy the music!"
}
User: "What's the weather like in New York?"
JSON: {
"action": "check weather",
"location": "New York",
"response": "I'm checking the weather in New York for you. One moment please."
}
Adapt the JSON structure to fit the context of the command, but always include the 'response' field.
"""
def process_command(command):
text=command
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": text}
]
)
return json.loads(response.choices[0].message.content)
def text_to_speech(text):
speech_file_path = Path(tempfile.gettempdir()) / "speech.mp3"
response = client.audio.speech.create(
model="tts-1",
voice="nova",
input=text
)
response.stream_to_file(speech_file_path)
return speech_file_path
st.title("Text Command Assistant")
# Text input for user command
command = st.text_input("Enter your command:")
if st.button("Process Command"):
if command:
# Process the text command
with st.spinner("Processing command..."):
result = process_command(command)
# Display JSON result
st.json(result)
# Extract response and convert to speech
response_text = result["response"]
with st.spinner("Generating speech..."):
speech_file = text_to_speech(response_text)
# Play the generated speech
st.audio(str(speech_file))
else:
st.warning("Please enter a command.")
st.markdown("---")
st.write("Created by Shrestha Singh")