-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
74 lines (57 loc) · 2.44 KB
/
app.py
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
import streamlit as st
import urllib.request
import json
import os
import ssl
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
AZURE_ENDPOINT_KEY = os.environ['AZURE_ENDPOINT_KEY']
def allowSelfSignedHttps(allowed):
# bypass the server certificate verification on the client side
if allowed and not os.environ.get('PYTHONHTTPSVERIFY', '') and getattr(ssl, '_create_unverified_context', None):
ssl._create_default_https_context = ssl._create_unverified_context
def main():
allowSelfSignedHttps(True)
st.title("Azure Prompt Flow Chat Interface")
# Initialize chat history
if "chat_history" not in st.session_state:
st.session_state.chat_history = []
# Display chat history
for interaction in st.session_state.chat_history:
if interaction["inputs"]["question"]:
with st.chat_message("user"):
st.write(interaction["inputs"]["question"])
if interaction["outputs"]["answer"]:
with st.chat_message("assistant"):
st.write(interaction["outputs"]["answer"])
# React to user input
if user_input := st.chat_input("Ask me anything..."):
# Display user message in chat message container
st.chat_message("user").markdown(user_input)
# Query API
data = {"chat_history": st.session_state.chat_history, 'question' : user_input}
body = json.dumps(data).encode('utf-8')
url = 'https://shahml-hhrub.eastus.inference.ml.azure.com/score'
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {AZURE_ENDPOINT_KEY}',
'azureml-model-deployment': 'shahml-hhrub-1'
}
req = urllib.request.Request(url, body, headers)
try:
response = urllib.request.urlopen(req)
response_data = json.loads(response.read().decode('utf-8'))
# render
with st.chat_message("assistant"):
st.markdown(response_data['answer'])
# add user input to chat history
st.session_state.chat_history.append(
{"inputs": {"question": user_input},
"outputs": {"answer": response_data['answer']}}
)
except urllib.error.HTTPError as error:
st.error(f"The request failed with status code: {error.code}")
st.text(error.read().decode("utf8", 'ignore'))
if __name__ == "__main__":
main()