-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
77 lines (62 loc) · 2.9 KB
/
main.py
File metadata and controls
77 lines (62 loc) · 2.9 KB
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
import streamlit as st
from audiorecorder import audiorecorder
import numpy as np
import asyncio
from utils.model_schema import Role, Message
from utils.func_tools import chatgpt_completion, find_embedding_candidates, load_transformers, transcrire_audio
from streamlit_chat import message
import os
st.set_page_config(page_title="NexAI English Tutor", page_icon=":books:", layout="wide")
st.title("NexAI English Tutor Chat")
st.markdown("### Welcome to your English Tutor! 👋📚")
openai_api_key = st.sidebar.text_input("OpenAI API Key", type="password")
with st.sidebar:
audio = audiorecorder("၊၊||၊၊၊||၊၊၊Play၊၊||၊၊၊||၊", "🎙️Stop🔴", show_visualizer=True)
if 'transformer' not in st.session_state:
st.session_state.transformer = None
if 'history' not in st.session_state:
st.session_state.history = []
if 'is_recording' not in st.session_state:
st.session_state.is_recording = False
async def initialize_transformer():
st.session_state.transformer = load_transformers(model_name="all-MiniLM-L6-v2", cache_folder="models_cache")
def get_response(query):
query_embedding = st.session_state.transformer.encode(query)
paragraphes = find_embedding_candidates(query_embedding=query_embedding, top_k=7)
context = "\n\n".join(paragraphes)
completion_response = chatgpt_completion(context, query, st.session_state.history, api_key=openai_api_key)
response = ""
for chunk in completion_response:
content = chunk.choices[0].delta.content
if content is not None:
response += content
st.session_state.history.append({"role": "assistant", "content": response})
return response
if st.session_state.transformer is None:
asyncio.run(initialize_transformer())
for message in st.session_state.history:
if message["role"] == "user":
st.write(f"**Vous:** {message['content']}")
else:
st.write(f"**NexAI Tutor:** {message['content']}")
user_query = st.chat_input("Discutez !")
if user_query:
st.session_state.history.append({"role": "user", "content": user_query})
st.write(f"**Vous:** {user_query}")
with st.spinner(text="..."):
response = get_response(user_query)
st.write(f"**NexAI Tutor:** {response}")
if len(audio) > 0:
st.session_state.is_recording = True
st.audio(audio.export().read())
audio.export("utils/audio_tutor/audio.wav", format="wav")
st.session_state.is_recording = False
with st.spinner("Transcription..."):
transcription = transcrire_audio("utils/audio_tutor/audio.wav", api_key=openai_api_key)
st.session_state.history.append({"role": "user", "content": transcription})
st.write(f"**Vous:** {transcription}")
response = get_response(transcription)
st.write(f"**NexAI Tutor:** {response}")
os.remove("utils/audio_tutor/audio.wav")
else:
st.write("Hello !")