ghengx commited on
Commit
682b4a8
1 Parent(s): 24e8108
Files changed (3) hide show
  1. .DS_Store +0 -0
  2. main.py +68 -0
  3. requirements.txt +41 -0
.DS_Store ADDED
Binary file (6.15 kB). View file
 
main.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import streamlit as st
4
+ import random
5
+ import time
6
+ import requests
7
+ from dotenv import load_dotenv
8
+
9
+ load_dotenv()
10
+
11
+ conversation_id = ''
12
+
13
+ # Streamed response emulator
14
+ def response_generator(query):
15
+ global conversation_id
16
+ word = ''
17
+
18
+ res =requests.post(
19
+ f'{os.environ['api_url']}',
20
+ headers={
21
+ 'Content-Type': 'application/json',
22
+ 'Authorization': f'Bearer {os.environ['api_key']}'
23
+ },
24
+ json={
25
+ "inputs": {},
26
+ "query": query,
27
+ "response_mode": "blocking",
28
+ "conversation_id": conversation_id,
29
+ "user": "abc-123",
30
+ }
31
+ )
32
+ if res.status_code == 200:
33
+ data = json.loads(res.text)
34
+
35
+ conversation_id = data['conversation_id']
36
+
37
+ answer = data.get('answer','Unable to answer at the moment')
38
+ else:
39
+ answer = 'Error'
40
+
41
+ for word in answer.split():
42
+ yield word + " "
43
+ time.sleep(0.05)
44
+
45
+ st.title("Ask Amy")
46
+
47
+ # Initialize chat history
48
+ if "messages" not in st.session_state:
49
+ st.session_state.messages = []
50
+
51
+ # Display chat messages from history on app rerun
52
+ for message in st.session_state.messages:
53
+ with st.chat_message(message["role"]):
54
+ st.markdown(message["content"])
55
+
56
+ # Accept user input
57
+ if prompt := st.chat_input("What is up?"):
58
+ # Add user message to chat history
59
+ st.session_state.messages.append({"role": "user", "content": prompt})
60
+ # Display user message in chat message container
61
+ with st.chat_message("user"):
62
+ st.markdown(prompt)
63
+
64
+ # Display assistant response in chat message container
65
+ with st.chat_message("assistant"):
66
+ response = st.write_stream(response_generator(prompt))
67
+ # Add assistant response to chat history
68
+ st.session_state.messages.append({"role": "assistant", "content": response})
requirements.txt ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ altair==5.4.1
2
+ attrs==24.2.0
3
+ blinker==1.8.2
4
+ cachetools==5.5.0
5
+ certifi==2024.8.30
6
+ charset-normalizer==3.3.2
7
+ click==8.1.7
8
+ gitdb==4.0.11
9
+ GitPython==3.1.43
10
+ idna==3.8
11
+ Jinja2==3.1.4
12
+ jsonschema==4.23.0
13
+ jsonschema-specifications==2023.12.1
14
+ markdown-it-py==3.0.0
15
+ MarkupSafe==2.1.5
16
+ mdurl==0.1.2
17
+ narwhals==1.6.0
18
+ numpy==2.1.0
19
+ packaging==24.1
20
+ pandas==2.2.2
21
+ pillow==10.4.0
22
+ protobuf==5.28.0
23
+ pyarrow==17.0.0
24
+ pydeck==0.9.1
25
+ Pygments==2.18.0
26
+ python-dateutil==2.9.0.post0
27
+ python-dotenv==1.0.1
28
+ pytz==2024.1
29
+ referencing==0.35.1
30
+ requests==2.32.3
31
+ rich==13.8.0
32
+ rpds-py==0.20.0
33
+ six==1.16.0
34
+ smmap==5.0.1
35
+ streamlit==1.38.0
36
+ tenacity==8.5.0
37
+ toml==0.10.2
38
+ tornado==6.4.1
39
+ typing_extensions==4.12.2
40
+ tzdata==2024.1
41
+ urllib3==2.2.2