-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
89 lines (69 loc) · 2.8 KB
/
app.py
File metadata and controls
89 lines (69 loc) · 2.8 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
78
79
80
81
82
83
84
85
86
87
88
89
# Importing required packages
import streamlit as st
import time
from openai import OpenAI
st.set_page_config(page_title="Government Schemes for Startups",page_icon="🤖",layout='centered')
st.markdown("<h3 style='background:#0284fe;padding:20px;border-radius:10px;text-align:center;'>Startup Assist : Government Schemes for Startups</h3>",
unsafe_allow_html=True)
st.markdown("")
# Set your OpenAI API key and assistant ID here
api_key = st.secrets["openai_apikey"]
assistant_id = st.secrets["assistant_id"]
# Set openAI client , Assistant AI and thread
@st.cache_resource
def load_openai_client_and_assistant():
client = OpenAI(api_key=api_key)
my_assistant = client.beta.assistants.retrieve(assistant_id)
thread = client.beta.threads.create()
return client, my_assistant, thread
client, my_assistant, assistant_thread = load_openai_client_and_assistant()
# check in loop if assistant ai parse our request
def wait_on_run(run, thread):
while run.status == "queued" or run.status == "in_progress":
run = client.beta.threads.runs.retrieve(
thread_id=thread.id,
run_id=run.id,
)
time.sleep(0.5)
return run
# initiate assistant ai response
def get_assistant_response(user_input=""):
message = client.beta.threads.messages.create(
thread_id=assistant_thread.id,
role="user",
content=user_input,
)
run = client.beta.threads.runs.create(
thread_id=assistant_thread.id,
assistant_id=assistant_id,
)
run = wait_on_run(run, assistant_thread)
# Retrieve all the messages added after our last user message
messages = client.beta.threads.messages.list(
thread_id=assistant_thread.id, order="asc", after=message.id
)
return messages.data[0].content[0].text.value
video_file = open('myvideo.mp4', 'rb')
video_bytes = video_file.read()
st.video(video_bytes)
if "messages" not in st.session_state:
st.session_state.messages = []
# Display chat messages from history on app rerun
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# React to user input
if prompt := st.chat_input("You can ask me questions about start ups."):
# Display user message in chat message container
with st.chat_message("user"):
st.markdown(prompt)
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
# Display assistant response in chat message container
with st.chat_message("assistant"):
response = get_assistant_response(prompt)
st.markdown(response)
# Add assistant response to chat history
st.session_state.messages.append({"role": "assistant","content": response})
if st.button("Clear chat"):
st.session_state.clear()