-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage1.py
More file actions
72 lines (56 loc) · 2.31 KB
/
page1.py
File metadata and controls
72 lines (56 loc) · 2.31 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
import streamlit as st
from utils.model_schema import Role, Message
from utils.prompt_manager import build_system_settings
import asyncio
import os
st.set_page_config(
page_title="NexAI English Tutor - TOEFL Reading", page_icon=":books:", layout="wide"
)
st.title("NexAI English Tutor - TOEFL Reading")
st.markdown("### Ask your questions about TOEFL Reading exercises!")
# --- Load Transformer Model ---
if "transformer" not in st.session_state:
st.session_state.transformer = None
if "history" not in st.session_state:
st.session_state.history = []
if "exercise_context" not in st.session_state:
st.session_state.exercise_context = ""
st.session_state.exercise_context = "TOEFL Reading"
from utils.func_tools import load_transformers, chatgpt_completion, find_embedding_candidates
async def initialize_transformer():
st.session_state.transformer = load_transformers(
model_name="all-MiniLM-L6-v2", cache_folder="models_cache"
)
def get_response(query):
completion_response = chatgpt_completion(
st.session_state.exercise_context, query, st.session_state.history
)
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())
# --- Display Chat History and Response ---
if st.session_state.exercise_context:
# Display previous interactions
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 Input
user_query = st.chat_input(
f"Ask your question about {st.session_state.exercise_context} exercises:"
)
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}")
# --- Navigate to Other Pages ---
st.button("Go back to Main Page", on_click=lambda: st.session_state.exercise_context, args=(""))