Feat: adds front and infra
This commit is contained in:
49
front/app/front.py
Normal file
49
front/app/front.py
Normal file
@@ -0,0 +1,49 @@
|
||||
import streamlit as st
|
||||
from typing import Set
|
||||
import requests
|
||||
import json
|
||||
import yaml
|
||||
import st_auth
|
||||
import boto3
|
||||
|
||||
authenticator=st_auth.get_authenticator()
|
||||
from botocore.exceptions import ClientError
|
||||
|
||||
st_auth.st_authenticate(authenticator)
|
||||
st.header("Chatbot Editais")
|
||||
url="https://dcrwpxp8lj-vpce-08d695f6315d510eb.execute-api.us-east-1.amazonaws.com/dev"
|
||||
payload=[]
|
||||
message_history=[]
|
||||
if "user_prompt_history" not in st.session_state:
|
||||
st.session_state["user_prompt_history"]=[]
|
||||
if "chat_answer_history" not in st.session_state:
|
||||
st.session_state["chat_answer_history"]=[]
|
||||
if "chat_history" not in st.session_state:
|
||||
st.session_state["chat_history"] = []
|
||||
prompt=st.text_input("Prompt",placeholder="Enter your prompt here..")
|
||||
def create_sources_string(source_urls: Set[str])->str:
|
||||
if not source_urls:
|
||||
return ""
|
||||
source_list=list(source_urls)
|
||||
source_list.sort()
|
||||
sources_string="source:\n"
|
||||
for i, source in enumerate(source_list):
|
||||
sources_string+=f"{i+1}, {source}\n"
|
||||
return sources_string
|
||||
if prompt:
|
||||
with st.spinner("Generating response.."):
|
||||
payload=st.session_state["chat_history"]+[{"role":"user","content":prompt}]
|
||||
content={"message":payload}
|
||||
headers={"Content-type":"application/json","x-api-key":json.loads(st_auth.get_secret())['api-gateway-api-key']}
|
||||
generated_response=json.loads(requests.post(url,json=content,headers=headers).text)['json']
|
||||
#generated_response=[{"role":"user","content":prompt}]
|
||||
# sources= set([doc.metadata["source"] for doc in generated_response['context']])
|
||||
#formatted_response=f"{generated_response['answer']} \n\n {create_sources_string(sources)}"
|
||||
formatted_response=generated_response
|
||||
st.session_state["user_prompt_history"].append(prompt)
|
||||
st.session_state["chat_answer_history"].append(formatted_response)
|
||||
st.session_state["chat_history"]=st.session_state["chat_history"]+[{"role":"user","content":prompt}]
|
||||
if st.session_state["chat_answer_history"]:
|
||||
for generated_response, user_query in zip(st.session_state["chat_answer_history"],st.session_state["user_prompt_history"]):
|
||||
st.chat_message("user").write(user_query)
|
||||
st.chat_message("assistant").write(generated_response)
|
||||
Reference in New Issue
Block a user