Spaces:
Sleeping
Sleeping
Asaad Almutareb
commited on
Commit
·
3b24cdb
1
Parent(s):
5c2babb
hf fixes
Browse files- app_gui.py +75 -61
- requirements.txt +3 -1
app_gui.py
CHANGED
@@ -1,76 +1,90 @@
|
|
1 |
# Import Gradio for UI, along with other necessary libraries
|
2 |
import gradio as gr
|
|
|
3 |
from rag_app.agents.react_agent import agent_executor
|
4 |
# need to import the qa!
|
5 |
|
6 |
-
|
7 |
-
def add_text(history, text):
|
8 |
-
# Append the new text to the history with a placeholder for the response
|
9 |
-
history = history + [(text, None)]
|
10 |
-
return history, ""
|
11 |
|
12 |
-
|
13 |
-
def bot(history):
|
14 |
-
# Obtain the response from the 'infer' function using the latest input
|
15 |
-
response = infer(history[-1][0], history)
|
16 |
-
#sources = [doc.metadata.get("source") for doc in response['source_documents']]
|
17 |
-
#src_list = '\n'.join(sources)
|
18 |
-
#print_this = response['result'] + "\n\n\n Sources: \n\n\n" + src_list
|
19 |
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
#
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
25 |
|
26 |
-
# Function to infer the response using the RAG model
|
27 |
-
def infer(question, history):
|
28 |
-
# Use the question and history to query the RAG model
|
29 |
-
#result = qa({"query": question, "history": history, "question": question})
|
30 |
-
try:
|
31 |
-
result = agent_executor.invoke(
|
32 |
-
{
|
33 |
-
"input": question,
|
34 |
-
"chat_history": history
|
35 |
-
}
|
36 |
-
)
|
37 |
-
return result
|
38 |
-
except Exception:
|
39 |
-
raise gr.Error("Model is Overloaded, Please retry later!")
|
40 |
|
41 |
-
#
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
-
# HTML content for the Gradio interface title
|
47 |
-
title = """
|
48 |
-
<div style="text-align:left;">
|
49 |
-
|
50 |
-
</div>
|
51 |
-
"""
|
52 |
|
53 |
-
# Building the Gradio interface
|
54 |
-
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
63 |
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
-
#
|
69 |
-
|
70 |
-
bot, chatbot, chatbot
|
71 |
-
)
|
72 |
-
# Define the action for the clear button
|
73 |
-
clear.click(lambda: None, None, chatbot, queue=False)
|
74 |
|
75 |
-
|
76 |
-
demo.launch(share=False, debug=True)
|
|
|
1 |
# Import Gradio for UI, along with other necessary libraries
|
2 |
import gradio as gr
|
3 |
+
from fastapi import FastAPI
|
4 |
from rag_app.agents.react_agent import agent_executor
|
5 |
# need to import the qa!
|
6 |
|
7 |
+
app = FastAPI()
|
|
|
|
|
|
|
|
|
8 |
|
9 |
+
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
# Function to add a new input to the chat history
|
12 |
+
def add_text(history, text):
|
13 |
+
# Append the new text to the history with a placeholder for the response
|
14 |
+
history = history + [(text, None)]
|
15 |
+
return history, ""
|
16 |
|
17 |
+
# Function representing the bot's response mechanism
|
18 |
+
def bot(history):
|
19 |
+
# Obtain the response from the 'infer' function using the latest input
|
20 |
+
response = infer(history[-1][0], history)
|
21 |
+
#sources = [doc.metadata.get("source") for doc in response['source_documents']]
|
22 |
+
#src_list = '\n'.join(sources)
|
23 |
+
#print_this = response['result'] + "\n\n\n Sources: \n\n\n" + src_list
|
24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
+
#history[-1][1] = print_this #response['answer']
|
27 |
+
# Update the history with the bot's response
|
28 |
+
history[-1][1] = response['output']
|
29 |
+
return history
|
30 |
+
|
31 |
+
# Function to infer the response using the RAG model
|
32 |
+
def infer(question, history):
|
33 |
+
# Use the question and history to query the RAG model
|
34 |
+
#result = qa({"query": question, "history": history, "question": question})
|
35 |
+
try:
|
36 |
+
result = agent_executor.invoke(
|
37 |
+
{
|
38 |
+
"input": question,
|
39 |
+
"chat_history": history
|
40 |
+
}
|
41 |
+
)
|
42 |
+
return result
|
43 |
+
except Exception:
|
44 |
+
raise gr.Error("Model is Overloaded, Please retry later!")
|
45 |
+
|
46 |
+
def vote(data: gr.LikeData):
|
47 |
+
if data.liked:
|
48 |
+
print("You upvoted this response: " + data.value)
|
49 |
+
else:
|
50 |
+
print("You downvoted this response: " + data.value)
|
51 |
+
|
52 |
+
# CSS styling for the Gradio interface
|
53 |
+
css = """
|
54 |
+
#col-container {max-width: 700px; margin-left: auto; margin-right: auto;}
|
55 |
+
"""
|
56 |
|
57 |
+
# HTML content for the Gradio interface title
|
58 |
+
title = """
|
59 |
+
<div style="text-align:left;">
|
60 |
+
<p>Hello, I BotTina 2.0, your intelligent AI assistant. I can help you explore Wuerttembergische Versicherungs products.<br />
|
61 |
+
</div>
|
62 |
+
"""
|
63 |
|
64 |
+
# Building the Gradio interface
|
65 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
66 |
+
with gr.Column(elem_id="col-container"):
|
67 |
+
gr.HTML(title) # Add the HTML title to the interface
|
68 |
+
chatbot = gr.Chatbot([], elem_id="chatbot",
|
69 |
+
label="BotTina 2.0",
|
70 |
+
bubble_full_width=False,
|
71 |
+
avatar_images=(None, "https://dacodi-production.s3.amazonaws.com/store/87bc00b6727589462954f2e3ff6f531c.png"),
|
72 |
+
height=680,) # Initialize the chatbot component
|
73 |
+
chatbot.like(vote, None, None)
|
74 |
+
clear = gr.Button("Clear") # Add a button to clear the chat
|
75 |
|
76 |
+
# Create a row for the question input
|
77 |
+
with gr.Row():
|
78 |
+
question = gr.Textbox(label="Question", placeholder="Type your question and hit Enter ")
|
79 |
+
|
80 |
+
# Define the action when the question is submitted
|
81 |
+
question.submit(add_text, [chatbot, question], [chatbot, question], queue=False).then(
|
82 |
+
bot, chatbot, chatbot
|
83 |
+
)
|
84 |
+
# Define the action for the clear button
|
85 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
86 |
|
87 |
+
# Launch the Gradio demo interface
|
88 |
+
demo.queue().launch(share=False, debug=True)
|
|
|
|
|
|
|
|
|
89 |
|
90 |
+
app = gr.mount_gradio_app(app, demo, path="/")
|
|
requirements.txt
CHANGED
@@ -14,4 +14,6 @@
|
|
14 |
gradio
|
15 |
boto3
|
16 |
rich
|
17 |
-
sqlmodel
|
|
|
|
|
|
14 |
gradio
|
15 |
boto3
|
16 |
rich
|
17 |
+
sqlmodel
|
18 |
+
fastapi
|
19 |
+
uvicron
|