Spaces:
Sleeping
Sleeping
added db to app_gui
Browse files- app_gui.py +37 -98
app_gui.py
CHANGED
|
@@ -1,19 +1,12 @@
|
|
| 1 |
# Import Gradio for UI, along with other necessary libraries
|
| 2 |
import gradio as gr
|
| 3 |
-
from fastapi import FastAPI
|
| 4 |
-
from fastapi import FastAPI
|
| 5 |
-
from rag_app.agents.react_agent import agent_executor, llm
|
| 6 |
-
from rag_app.chains import user_response_sentiment_prompt
|
| 7 |
-
from typing import Dict
|
| 8 |
-
import re
|
| 9 |
-
from rag_app.utils.utils import extract_responses
|
| 10 |
from rag_app.loading_data.load_S3_vector_stores import get_chroma_vs
|
| 11 |
from rag_app.agents.react_agent import agent_executor
|
| 12 |
-
|
| 13 |
|
| 14 |
-
app = FastAPI()
|
| 15 |
get_chroma_vs()
|
| 16 |
|
|
|
|
| 17 |
if __name__ == "__main__":
|
| 18 |
|
| 19 |
# Function to add a new input to the chat history
|
|
@@ -21,29 +14,12 @@ if __name__ == "__main__":
|
|
| 21 |
# Append the new text to the history with a placeholder for the response
|
| 22 |
history = history + [(text, None)]
|
| 23 |
return history, ""
|
| 24 |
-
# Function to add a new input to the chat history
|
| 25 |
-
def add_text(history, text):
|
| 26 |
-
# Append the new text to the history with a placeholder for the response
|
| 27 |
-
history = history + [(text, None)]
|
| 28 |
-
return history, ""
|
| 29 |
-
|
| 30 |
-
# Function representing the bot's response mechanism
|
| 31 |
-
def bot(history):
|
| 32 |
-
# Obtain the response from the 'infer' function using the latest input
|
| 33 |
-
response = infer(history[-1][0], history)
|
| 34 |
-
#sources = [doc.metadata.get("source") for doc in response['source_documents']]
|
| 35 |
-
#src_list = '\n'.join(sources)
|
| 36 |
-
#print_this = response['result'] + "\n\n\n Sources: \n\n\n" + src_list
|
| 37 |
|
| 38 |
-
|
| 39 |
-
#history[-1][1] = print_this #response['answer']
|
| 40 |
-
# Update the history with the bot's response
|
| 41 |
-
history[-1][1] = response['output']
|
| 42 |
-
return history
|
| 43 |
# Function representing the bot's response mechanism
|
| 44 |
def bot(history):
|
| 45 |
# Obtain the response from the 'infer' function using the latest input
|
| 46 |
response = infer(history[-1][0], history)
|
|
|
|
| 47 |
history[-1][1] = response['output']
|
| 48 |
return history
|
| 49 |
|
|
@@ -60,37 +36,7 @@ if __name__ == "__main__":
|
|
| 60 |
)
|
| 61 |
return result
|
| 62 |
except Exception:
|
| 63 |
-
raise gr.
|
| 64 |
-
|
| 65 |
-
def vote(data: gr.LikeData):
|
| 66 |
-
if data.liked:
|
| 67 |
-
print("You upvoted this response: " + data.value)
|
| 68 |
-
else:
|
| 69 |
-
print("You downvoted this response: " + data.value)
|
| 70 |
-
# Function to infer the response using the RAG model
|
| 71 |
-
def infer(question, history):
|
| 72 |
-
# Use the question and history to query the RAG model
|
| 73 |
-
#result = qa({"query": question, "history": history, "question": question})
|
| 74 |
-
# try:
|
| 75 |
-
# data = user_sentiment_chain.invoke({"user_reponse":question})
|
| 76 |
-
# responses = extract_responses(data)
|
| 77 |
-
# if responses['AI'] == "1":
|
| 78 |
-
# pass
|
| 79 |
-
# # Do important stuff here plox
|
| 80 |
-
# # store into database
|
| 81 |
-
# except Exception as e:
|
| 82 |
-
# raise e
|
| 83 |
-
try:
|
| 84 |
-
result = agent_executor.invoke(
|
| 85 |
-
{
|
| 86 |
-
"input": question,
|
| 87 |
-
"chat_history": history
|
| 88 |
-
}
|
| 89 |
-
)
|
| 90 |
-
return result
|
| 91 |
-
except Exception as e:
|
| 92 |
-
# raise gr.Error("Model is Overloaded, Please retry later!")
|
| 93 |
-
raise e
|
| 94 |
|
| 95 |
def vote(data: gr.LikeData):
|
| 96 |
if data.liked:
|
|
@@ -98,13 +44,14 @@ if __name__ == "__main__":
|
|
| 98 |
else:
|
| 99 |
print("You downvoted this response: ")
|
| 100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
# CSS styling for the Gradio interface
|
| 102 |
css = """
|
| 103 |
-
#col-container {max-width:
|
| 104 |
-
"""
|
| 105 |
-
# CSS styling for the Gradio interface
|
| 106 |
-
css = """
|
| 107 |
-
#col-container {max-width: 700px; margin-left: auto; margin-right: auto;}
|
| 108 |
"""
|
| 109 |
|
| 110 |
# HTML content for the Gradio interface title
|
|
@@ -113,59 +60,51 @@ if __name__ == "__main__":
|
|
| 113 |
<p>Hello, I BotTina 2.0, your intelligent AI assistant. I can help you explore Wuerttembergische Versicherungs products.<br />
|
| 114 |
</div>
|
| 115 |
"""
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
"""
|
| 122 |
|
| 123 |
# Building the Gradio interface
|
| 124 |
-
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 125 |
with gr.Column(elem_id="col-container"):
|
| 126 |
-
gr.HTML(
|
| 127 |
chatbot = gr.Chatbot([], elem_id="chatbot",
|
| 128 |
-
label="
|
| 129 |
bubble_full_width=False,
|
| 130 |
avatar_images=(None, "https://dacodi-production.s3.amazonaws.com/store/87bc00b6727589462954f2e3ff6f531c.png"),
|
| 131 |
height=680,) # Initialize the chatbot component
|
| 132 |
chatbot.like(vote, None, None)
|
| 133 |
-
clear = gr.Button("Clear") # Add a button to clear the chat
|
| 134 |
-
# Building the Gradio interface
|
| 135 |
-
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 136 |
-
with gr.Column(elem_id="col-container"):
|
| 137 |
-
gr.HTML(title) # Add the HTML title to the interface
|
| 138 |
-
chatbot = gr.Chatbot([], elem_id="chatbot",
|
| 139 |
-
label="BotTina 2.0",
|
| 140 |
-
bubble_full_width=False,
|
| 141 |
-
avatar_images=(None, "https://dacodi-production.s3.amazonaws.com/store/87bc00b6727589462954f2e3ff6f531c.png"),
|
| 142 |
-
height=680,) # Initialize the chatbot component
|
| 143 |
-
chatbot.like(vote, None, None)
|
| 144 |
-
clear = gr.Button("Clear") # Add a button to clear the chat
|
| 145 |
|
| 146 |
# Create a row for the question input
|
| 147 |
with gr.Row():
|
| 148 |
-
question = gr.Textbox(label="Question", placeholder="Type your question and hit Enter ")
|
| 149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
with gr.Row():
|
| 151 |
-
|
| 152 |
|
| 153 |
# Define the action when the question is submitted
|
| 154 |
question.submit(add_text, [chatbot, question], [chatbot, question], queue=False).then(
|
| 155 |
-
bot, chatbot, chatbot
|
| 156 |
-
)
|
|
|
|
| 157 |
# Define the action for the clear button
|
| 158 |
clear.click(lambda: None, None, chatbot, queue=False)
|
| 159 |
-
# Define the action when the question is submitted
|
| 160 |
-
question.submit(add_text, [chatbot, question], [chatbot, question], queue=False).then(
|
| 161 |
-
bot, chatbot, chatbot
|
| 162 |
-
)
|
| 163 |
-
# Define the action for the clear button
|
| 164 |
-
clear.click(lambda: None, None, chatbot, queue=False)
|
| 165 |
-
|
| 166 |
-
# Launch the Gradio demo interface
|
| 167 |
-
demo.queue().launch(share=False, debug=True)
|
| 168 |
|
| 169 |
-
app = gr.mount_gradio_app(app, demo, path="/")
|
| 170 |
# Launch the Gradio demo interface
|
| 171 |
demo.queue().launch(share=False, debug=True)
|
|
|
|
| 1 |
# Import Gradio for UI, along with other necessary libraries
|
| 2 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from rag_app.loading_data.load_S3_vector_stores import get_chroma_vs
|
| 4 |
from rag_app.agents.react_agent import agent_executor
|
| 5 |
+
from config import db
|
| 6 |
|
|
|
|
| 7 |
get_chroma_vs()
|
| 8 |
|
| 9 |
+
|
| 10 |
if __name__ == "__main__":
|
| 11 |
|
| 12 |
# Function to add a new input to the chat history
|
|
|
|
| 14 |
# Append the new text to the history with a placeholder for the response
|
| 15 |
history = history + [(text, None)]
|
| 16 |
return history, ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
# Function representing the bot's response mechanism
|
| 19 |
def bot(history):
|
| 20 |
# Obtain the response from the 'infer' function using the latest input
|
| 21 |
response = infer(history[-1][0], history)
|
| 22 |
+
print(response)
|
| 23 |
history[-1][1] = response['output']
|
| 24 |
return history
|
| 25 |
|
|
|
|
| 36 |
)
|
| 37 |
return result
|
| 38 |
except Exception:
|
| 39 |
+
raise gr.Warning("Model is Overloaded, please try again in a few minutes!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
def vote(data: gr.LikeData):
|
| 42 |
if data.liked:
|
|
|
|
| 44 |
else:
|
| 45 |
print("You downvoted this response: ")
|
| 46 |
|
| 47 |
+
def get_examples(input_text: str):
|
| 48 |
+
tmp_history = [(input_text, None)]
|
| 49 |
+
response = infer(input_text, tmp_history)
|
| 50 |
+
return response['output']
|
| 51 |
+
|
| 52 |
# CSS styling for the Gradio interface
|
| 53 |
css = """
|
| 54 |
+
#col-container {max-width: 1200px; margin-left: auto; margin-right: auto;}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
"""
|
| 56 |
|
| 57 |
# HTML content for the Gradio interface title
|
|
|
|
| 60 |
<p>Hello, I BotTina 2.0, your intelligent AI assistant. I can help you explore Wuerttembergische Versicherungs products.<br />
|
| 61 |
</div>
|
| 62 |
"""
|
| 63 |
+
head_style = """
|
| 64 |
+
<style>
|
| 65 |
+
@media (min-width: 1536px)
|
| 66 |
+
{
|
| 67 |
+
.gradio-container {
|
| 68 |
+
min-width: var(--size-full) !important;
|
| 69 |
+
}
|
| 70 |
+
}
|
| 71 |
+
</style>
|
| 72 |
"""
|
| 73 |
|
| 74 |
# Building the Gradio interface
|
| 75 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="InsurePal AI 🤵🏻♂️", head=head_style) as demo:
|
| 76 |
with gr.Column(elem_id="col-container"):
|
| 77 |
+
gr.HTML() # Add the HTML title to the interface
|
| 78 |
chatbot = gr.Chatbot([], elem_id="chatbot",
|
| 79 |
+
label="InsurePal AI",
|
| 80 |
bubble_full_width=False,
|
| 81 |
avatar_images=(None, "https://dacodi-production.s3.amazonaws.com/store/87bc00b6727589462954f2e3ff6f531c.png"),
|
| 82 |
height=680,) # Initialize the chatbot component
|
| 83 |
chatbot.like(vote, None, None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
# Create a row for the question input
|
| 86 |
with gr.Row():
|
| 87 |
+
question = gr.Textbox(label="Question", show_label=False, placeholder="Type your question and hit Enter ", scale=4)
|
| 88 |
+
send_btn = gr.Button(value="Send", variant="primary", scale=0)
|
| 89 |
+
with gr.Accordion(label="Beispiele", open=False):
|
| 90 |
+
#examples
|
| 91 |
+
examples = gr.Examples([
|
| 92 |
+
"Welche Versicherungen brauche ich als Student?",
|
| 93 |
+
"Wie melde ich einen Schaden?",
|
| 94 |
+
"Wie kann ich mich als Selbstständiger finanziell absichern?",
|
| 95 |
+
"Welche Versicherungen sollte ich für meine Vorsorge abschliessen?"
|
| 96 |
+
], inputs=[question], label="") #, cache_examples="lazy", fn=get_examples, outputs=[chatbot]
|
| 97 |
+
|
| 98 |
with gr.Row():
|
| 99 |
+
clear = gr.Button("Clear") # Add a button to clear the chat
|
| 100 |
|
| 101 |
# Define the action when the question is submitted
|
| 102 |
question.submit(add_text, [chatbot, question], [chatbot, question], queue=False).then(
|
| 103 |
+
bot, chatbot, chatbot)
|
| 104 |
+
send_btn.click(add_text, [chatbot, question], [chatbot, question], queue=False).then(
|
| 105 |
+
bot, chatbot, chatbot)
|
| 106 |
# Define the action for the clear button
|
| 107 |
clear.click(lambda: None, None, chatbot, queue=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
|
|
|
| 109 |
# Launch the Gradio demo interface
|
| 110 |
demo.queue().launch(share=False, debug=True)
|