dolzhansky / app.py
belyakoff's picture
Update app.py
bb7bc69 verified
import tempfile
from pathlib import Path
import openai
import streamlit as st
from streamlit_mic_recorder import mic_recorder
import os
openai.api_key = os.getenv('OPENAI_API_KEY')
st.set_page_config(page_title="", page_icon="🎙️", layout="wide")
st.markdown("<h1 style='text-align: center;'>🗣️ Покупатель Н. Должанский</h1>", unsafe_allow_html=True)
st.markdown("<p style='text-align: center;'>Показывает пользователю, как общаться</p>",
unsafe_allow_html=True)
if "chat_history" not in st.session_state:
st.session_state.chat_history = []
def read_file(path: str):
with open(Path(path), 'r', encoding='utf-8') as f:
return f.read()
prompt_1 = read_file('prompt_dolzhansky.txt')
prompt_2 = read_file('supervisor_prompt')
def transcribe_audio(audio_path):
with open(audio_path, "rb") as audio:
transcript = openai.audio.transcriptions.create(
model="whisper-1",
file=audio
)
return transcript.text
def chat_with_gpt(user_message, prompt):
if not user_message:
return ""
hist = []
for i in st.session_state.chat_history:
hist.append(
{'role': 'user', 'content': i['user']}
)
hist.append(
{'role': 'assistant', 'content': i['assistant']}
)
hist.append({'role': 'user', 'content': user_message})
response = openai.chat.completions.create(
model="gpt-4o",
messages=[{'role': 'system', 'content': prompt, }, ] + hist
)
gpt_reply = response.choices[0].message.content
return gpt_reply
def generate_audio(text):
tts_audio = openai.audio.speech.create(
model="tts-1",
voice="ash",
input=text
)
temp_audio_path = tempfile.mktemp(suffix=".mp3")
with open(temp_audio_path, "wb") as audio_file:
audio_file.write(tts_audio.read())
return temp_audio_path
with st.container():
for entry in st.session_state.get("chat_history", []):
with st.chat_message("user"):
st.markdown(entry["user"])
with st.chat_message("assistant"):
st.markdown(entry["assistant"])
try:
st.audio(entry["audio"], format="audio/mp3")
except:
continue
inp = st.text_input("💬 Введите сообщение:", "")
user_input = None
audio_data = mic_recorder(start_prompt="🎤 Нажмите для записи", stop_prompt="🛑 Остановить")
if audio_data is not None:
audio_path = tempfile.mktemp(suffix=".wav")
with open(audio_path, "wb") as f:
f.write(audio_data['bytes'])
text_from_audio = transcribe_audio(audio_path)
user_input = text_from_audio
if st.button("📨 Отправить"):
if inp:
response_text = chat_with_gpt(inp, prompt_1)
audio_path = generate_audio(response_text)
st.session_state.chat_history.append({
"user": inp,
"assistant": response_text,
"audio": audio_path
})
st.rerun()
if user_input:
response_text = chat_with_gpt(user_input, prompt_1)
audio_path = generate_audio(response_text)
st.session_state.chat_history.append({
"user": user_input,
"assistant": response_text,
"audio": audio_path
})
user_input = None
audio_data = None
st.rerun()
if st.button("📨 Получить оценку"):
response_text = chat_with_gpt('Напиши оценку продавца.', prompt_2)
st.session_state.chat_history.append({
"user": user_input,
"assistant": response_text,
})
user_input = None
audio_data = None
st.rerun()