|
import streamlit as st |
|
import json |
|
from openai import OpenAI |
|
from pathlib import Path |
|
import tempfile |
|
import os |
|
|
|
|
|
|
|
OpenAI.api_key = os.getenv("openai") |
|
|
|
|
|
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") |
|
|
|
|
|
command = st.text_input("Enter your command:") |
|
|
|
if st.button("Process Command"): |
|
if command: |
|
|
|
with st.spinner("Processing command..."): |
|
result = process_command(command) |
|
|
|
|
|
st.json(result) |
|
|
|
|
|
response_text = result["response"] |
|
with st.spinner("Generating speech..."): |
|
speech_file = text_to_speech(response_text) |
|
|
|
|
|
st.audio(str(speech_file)) |
|
else: |
|
st.warning("Please enter a command.") |
|
|
|
st.markdown("---") |
|
st.write("Created by Shrestha Singh") |
|
|
|
|