File size: 2,473 Bytes
6a14e1b 834700e 6a14e1b 7469aa6 6a14e1b 7469aa6 6a14e1b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
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
OpenAI.api_key = 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")
|