Spaces:
Sleeping
Sleeping
chat interface
Browse files
app.py
CHANGED
@@ -8,6 +8,7 @@ from graph_handler import query_graph_qa, plot_subgraph
|
|
8 |
from embed_handler import query_rag_qa
|
9 |
from evaluate import evaluate_llm, reasoning_graph, get_coupon
|
10 |
import base64
|
|
|
11 |
|
12 |
load_dotenv()
|
13 |
|
@@ -44,65 +45,18 @@ def query_tqa(query, search_level):
|
|
44 |
grag_response, grag_reference, grag_reference_text = query_graph_qa(
|
45 |
graph_rag_index, query, search_level
|
46 |
)
|
47 |
-
rag_response, rag_reference, rag_reference_text = query_rag_qa(
|
48 |
-
|
49 |
-
)
|
50 |
return (
|
51 |
grag_response,
|
52 |
-
grag_reference,
|
53 |
-
grag_reference_text,
|
54 |
-
rag_response,
|
55 |
-
rag_reference,
|
56 |
-
rag_reference_text,
|
57 |
)
|
58 |
|
59 |
-
|
60 |
-
# def eval_llm(query, rag_response, grag_response):
|
61 |
-
# """
|
62 |
-
# Evaluate the Graph-RAG and RAG responses using an LLM.
|
63 |
-
|
64 |
-
# Args:
|
65 |
-
# query (str): The query that was asked.
|
66 |
-
# rag_response (str): The response from the Vanilla-RAG model.
|
67 |
-
# grag_response (str): The response from the Graph-RAG model.
|
68 |
-
|
69 |
-
# Returns:
|
70 |
-
# str: The evaluation text on various criteria from the LLM.
|
71 |
-
# """
|
72 |
-
|
73 |
-
# if not query.strip() or not rag_response.strip() or not grag_response.strip():
|
74 |
-
# raise gr.Error("Please ask a query and get responses before evaluating.")
|
75 |
-
|
76 |
-
# eval_text = evaluate_llm(query, grag_response, rag_response)
|
77 |
-
# return eval_text
|
78 |
-
|
79 |
-
|
80 |
-
# def reason_and_plot(query, grag_response, grag_reference):
|
81 |
-
# """
|
82 |
-
# Get the reasoning graph for a query and plot the knowledge graph.
|
83 |
-
|
84 |
-
# Args:
|
85 |
-
# query (str): The query to ask the Graph-RAG.
|
86 |
-
# grag_response (str): The response from the Graph-RAG model.
|
87 |
-
# grag_reference (str): The reference text from the Graph-RAG model.
|
88 |
-
|
89 |
-
# Returns:
|
90 |
-
# tuple: The reasoning graph and the HTML to plot the knowledge graph.
|
91 |
-
# """
|
92 |
-
|
93 |
-
# if not query.strip() or not grag_response.strip() or not grag_reference.strip():
|
94 |
-
# raise gr.Error(
|
95 |
-
# "Please ask a query and get a Graph-RAG response before reasoning."
|
96 |
-
# )
|
97 |
-
|
98 |
-
# graph_reasoning = reasoning_graph(query, grag_response, grag_reference)
|
99 |
-
# escaped_html = plot_subgraph(grag_reference)
|
100 |
-
|
101 |
-
# iframe_html = f'<iframe srcdoc="{escaped_html}" width="100%" height="400px" frameborder="0"></iframe>'
|
102 |
-
|
103 |
-
# return graph_reasoning, iframe_html
|
104 |
-
|
105 |
-
|
106 |
def show_graph():
|
107 |
"""
|
108 |
Show the latest graph visualization in an iframe.
|
@@ -127,126 +81,25 @@ def show_graph():
|
|
127 |
return f"Error: {str(e)}"
|
128 |
|
129 |
|
130 |
-
def reveal_coupon(query, grag_response):
|
131 |
-
"""
|
132 |
-
Get the coupon from the query and response.
|
133 |
-
|
134 |
-
Args:
|
135 |
-
query (str): Query asked to Graph-RAG.
|
136 |
-
grag_response (str): Response from the Graph-RAG model.
|
137 |
-
|
138 |
-
Returns:
|
139 |
-
str: Coupon with reasoning.
|
140 |
-
"""
|
141 |
-
|
142 |
-
if not query.strip() or not grag_response.strip():
|
143 |
-
raise gr.Error("Please ask a query and get a response before revealing the coupon.")
|
144 |
-
|
145 |
-
coupon = get_coupon(query, grag_response)
|
146 |
-
return coupon
|
147 |
-
|
148 |
with gr.Blocks() as demo:
|
149 |
gr.Markdown("# Comfy Virtual Assistant")
|
|
|
|
|
|
|
150 |
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
ask_button = gr.Button("Ask Comfy", variant="primary")
|
159 |
-
|
160 |
-
examples = gr.Examples(
|
161 |
-
examples=[
|
162 |
-
["Recommend me an apple phone that has more than 10MP camera."],
|
163 |
-
["What is the price of Samsung Galaxy S24 Ultra 12/256Gb Titanium Gray"],
|
164 |
-
["I want a phone with 5000 mAH or more battery"],
|
165 |
-
],
|
166 |
-
inputs=[query_input],
|
167 |
-
)
|
168 |
|
169 |
-
with gr.Row():
|
170 |
-
with gr.Column():
|
171 |
-
gr.Markdown("### Graph-RAG")
|
172 |
-
grag_output = gr.Textbox(label="Response", lines=5)
|
173 |
-
# grag_reference = gr.Textbox(label="Triplets", lines=3)
|
174 |
-
# with gr.Accordion("Extracted Reference (Raw)", open=False):
|
175 |
-
# grag_reference_text = gr.Textbox(label="Raw Reference", lines=5)
|
176 |
-
|
177 |
-
# with gr.Column():
|
178 |
-
# gr.Markdown("### Vanilla RAG")
|
179 |
-
# rag_output = gr.Textbox(label="Response", lines=5)
|
180 |
-
# rag_reference = gr.Textbox(label="Extracted Reference", lines=3)
|
181 |
-
# with gr.Accordion("Extracted Reference (Raw)", open=False):
|
182 |
-
# rag_reference_text = gr.Textbox(label="Raw Reference", lines=5)
|
183 |
-
|
184 |
-
# gr.Markdown("### Coupon")
|
185 |
-
# with gr.Row():
|
186 |
-
# with gr.Column():
|
187 |
-
# coupon = gr.Text(label="Coupon", lines=1)
|
188 |
-
# with gr.Column():
|
189 |
-
# reveal = gr.Button("Reveal Coupon", variant="secondary")
|
190 |
-
|
191 |
-
# with gr.Row():
|
192 |
-
# gr.Markdown("### Evaluate and Compare")
|
193 |
-
|
194 |
-
# with gr.Row():
|
195 |
-
# eval_button = gr.Button("Evaluate LLMs", variant="secondary")
|
196 |
-
|
197 |
-
# grag_performance = gr.Textbox(label="Evaluation", lines=3)
|
198 |
-
|
199 |
-
# with gr.Row():
|
200 |
-
# gr.Markdown("### Graph Reasoning")
|
201 |
-
|
202 |
-
# with gr.Row():
|
203 |
-
# reason_button = gr.Button("Get Graph Reasoning", variant="secondary")
|
204 |
-
|
205 |
-
# with gr.Row():
|
206 |
-
# with gr.Column():
|
207 |
-
# grag_reasoning = gr.Textbox(label="Graph-RAG Reasoning", lines=5)
|
208 |
-
# with gr.Column():
|
209 |
-
# subgraph_plot = gr.HTML()
|
210 |
|
211 |
with gr.Row():
|
212 |
plot_button = gr.Button("Plot Knowledge Graph", variant="secondary")
|
213 |
|
214 |
kg_output = gr.HTML()
|
215 |
|
216 |
-
ask_button.click(
|
217 |
-
query_tqa,
|
218 |
-
inputs=[query_input, search_level],
|
219 |
-
outputs=[
|
220 |
-
grag_output,
|
221 |
-
# grag_reference,
|
222 |
-
# grag_reference_text,
|
223 |
-
# rag_output,
|
224 |
-
# rag_reference,
|
225 |
-
# rag_reference_text,
|
226 |
-
],
|
227 |
-
)
|
228 |
-
|
229 |
-
# eval_button.click(
|
230 |
-
# eval_llm,
|
231 |
-
# inputs=[query_input, rag_output, grag_output],
|
232 |
-
# outputs=[grag_performance],
|
233 |
-
# )
|
234 |
-
|
235 |
-
# reason_button.click(
|
236 |
-
# reason_and_plot,
|
237 |
-
# inputs=[query_input, grag_output, grag_reference],
|
238 |
-
# outputs=[grag_reasoning, subgraph_plot],
|
239 |
-
# )
|
240 |
-
|
241 |
-
plot_button.click(
|
242 |
-
show_graph,
|
243 |
-
outputs=[kg_output],
|
244 |
-
)
|
245 |
-
|
246 |
-
# reveal.click(
|
247 |
-
# reveal_coupon,
|
248 |
-
# inputs=[query_input, grag_output],
|
249 |
-
# outputs=[coupon],
|
250 |
-
# )
|
251 |
|
252 |
demo.launch(auth=(os.getenv("ID"), os.getenv("PASS")), share=False)
|
|
|
8 |
from embed_handler import query_rag_qa
|
9 |
from evaluate import evaluate_llm, reasoning_graph, get_coupon
|
10 |
import base64
|
11 |
+
import time
|
12 |
|
13 |
load_dotenv()
|
14 |
|
|
|
45 |
grag_response, grag_reference, grag_reference_text = query_graph_qa(
|
46 |
graph_rag_index, query, search_level
|
47 |
)
|
48 |
+
# rag_response, rag_reference, rag_reference_text = query_rag_qa(
|
49 |
+
# rag_index, query, search_level
|
50 |
+
# )
|
51 |
return (
|
52 |
grag_response,
|
53 |
+
# grag_reference,
|
54 |
+
# grag_reference_text,
|
55 |
+
# rag_response,
|
56 |
+
# rag_reference,
|
57 |
+
# rag_reference_text,
|
58 |
)
|
59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
def show_graph():
|
61 |
"""
|
62 |
Show the latest graph visualization in an iframe.
|
|
|
81 |
return f"Error: {str(e)}"
|
82 |
|
83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
with gr.Blocks() as demo:
|
85 |
gr.Markdown("# Comfy Virtual Assistant")
|
86 |
+
chatbot = gr.Chatbot()
|
87 |
+
msg = gr.Textbox(label="Input Your Query")
|
88 |
+
clear = gr.ClearButton([msg, chatbot])
|
89 |
|
90 |
+
def respond(message, chat_history):
|
91 |
+
bot_message = query_tqa(message, 2)
|
92 |
+
chat_history.append((message, bot_message))
|
93 |
+
time.sleep(1)
|
94 |
+
return "", chat_history
|
95 |
+
|
96 |
+
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
|
99 |
with gr.Row():
|
100 |
plot_button = gr.Button("Plot Knowledge Graph", variant="secondary")
|
101 |
|
102 |
kg_output = gr.HTML()
|
103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
|
105 |
demo.launch(auth=(os.getenv("ID"), os.getenv("PASS")), share=False)
|