Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -45,7 +45,7 @@ phi_agent = create_phi_agent()
|
|
| 45 |
|
| 46 |
# Response Generation
|
| 47 |
def retrieve_and_generate_response(query, cross_encoder_choice, history=None):
|
| 48 |
-
print('
|
| 49 |
top_rerank = 25
|
| 50 |
top_k_rank = 20
|
| 51 |
|
|
@@ -55,7 +55,7 @@ def retrieve_and_generate_response(query, cross_encoder_choice, history=None):
|
|
| 55 |
try:
|
| 56 |
start_time = perf_counter()
|
| 57 |
query_vec = retriever.encode(query)
|
| 58 |
-
print('query vector
|
| 59 |
documents = table.search(query_vec, vector_column_name="vector").limit(top_rerank).to_list()
|
| 60 |
documents = [doc["text"] for doc in documents]
|
| 61 |
|
|
@@ -86,7 +86,22 @@ def retrieve_and_generate_response(query, cross_encoder_choice, history=None):
|
|
| 86 |
logger.error(f"Error in response generation: {e}")
|
| 87 |
return f"Error: {str(e)}"
|
| 88 |
|
| 89 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
with gr.Blocks(title="Science Chatbot", theme='gradio/soft') as chatbot_app:
|
| 91 |
with gr.Row():
|
| 92 |
with gr.Column(scale=10):
|
|
@@ -106,18 +121,13 @@ with gr.Blocks(title="Science Chatbot", theme='gradio/soft') as chatbot_app:
|
|
| 106 |
with gr.Column(scale=3):
|
| 107 |
gr.Image(value='logo.png', height=200, width=200, show_label=False)
|
| 108 |
|
| 109 |
-
# chatbot
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
# bubble_full_width=False,
|
| 117 |
-
|
| 118 |
-
# label="Conversation"
|
| 119 |
-
# )
|
| 120 |
-
chatbot=gr.Chatbot()
|
| 121 |
|
| 122 |
with gr.Row():
|
| 123 |
query = gr.Textbox(
|
|
@@ -136,32 +146,27 @@ with gr.Blocks(title="Science Chatbot", theme='gradio/soft') as chatbot_app:
|
|
| 136 |
)
|
| 137 |
clear = gr.Button("Clear")
|
| 138 |
|
| 139 |
-
|
| 140 |
-
print('function called')
|
| 141 |
-
if not query.strip():
|
| 142 |
-
gr.Warning("Please submit a non-empty question")
|
| 143 |
-
return "", history
|
| 144 |
-
response = retrieve_and_generate_response(query, cross_encoder, history)
|
| 145 |
-
history.append((query, ""))
|
| 146 |
-
for character in response:
|
| 147 |
-
history[-1] = (query, history[-1][1] + character)
|
| 148 |
-
time.sleep(0.02) # Streaming effect
|
| 149 |
-
yield "", history
|
| 150 |
-
return "", history
|
| 151 |
-
|
| 152 |
submit_btn.click(
|
| 153 |
-
chatbot_func,
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
)
|
| 156 |
-
# query.submit(
|
| 157 |
-
# chatbot_func,
|
| 158 |
-
# inputs=[query, cross_encoder, chatbot],
|
| 159 |
-
# outputs=[query, chatbot],
|
| 160 |
-
# queue=False
|
| 161 |
-
# )
|
| 162 |
-
# clear.click(
|
| 163 |
-
# lambda: ([], ""), None, [chatbot, query], queue=False
|
| 164 |
-
# )
|
| 165 |
|
| 166 |
examples = [
|
| 167 |
'CAN U SAY THE DIFFERENCES BETWEEN METALS AND NON METALS?',
|
|
|
|
| 45 |
|
| 46 |
# Response Generation
|
| 47 |
def retrieve_and_generate_response(query, cross_encoder_choice, history=None):
|
| 48 |
+
print('history:', history)
|
| 49 |
top_rerank = 25
|
| 50 |
top_k_rank = 20
|
| 51 |
|
|
|
|
| 55 |
try:
|
| 56 |
start_time = perf_counter()
|
| 57 |
query_vec = retriever.encode(query)
|
| 58 |
+
print('query vector:', query_vec)
|
| 59 |
documents = table.search(query_vec, vector_column_name="vector").limit(top_rerank).to_list()
|
| 60 |
documents = [doc["text"] for doc in documents]
|
| 61 |
|
|
|
|
| 86 |
logger.error(f"Error in response generation: {e}")
|
| 87 |
return f"Error: {str(e)}"
|
| 88 |
|
| 89 |
+
# Fixed chatbot function
|
| 90 |
+
def chatbot_func(query, cross_encoder, history):
|
| 91 |
+
print('function called')
|
| 92 |
+
if not query.strip():
|
| 93 |
+
gr.Warning("Please submit a non-empty question")
|
| 94 |
+
return "", history
|
| 95 |
+
|
| 96 |
+
response = retrieve_and_generate_response(query, cross_encoder, history)
|
| 97 |
+
history.append([query, ""])
|
| 98 |
+
|
| 99 |
+
for character in response:
|
| 100 |
+
history[-1][1] += character
|
| 101 |
+
time.sleep(0.02) # Streaming effect
|
| 102 |
+
yield "", history
|
| 103 |
+
|
| 104 |
+
# Gradio Interface with Fixed Implementation
|
| 105 |
with gr.Blocks(title="Science Chatbot", theme='gradio/soft') as chatbot_app:
|
| 106 |
with gr.Row():
|
| 107 |
with gr.Column(scale=10):
|
|
|
|
| 121 |
with gr.Column(scale=3):
|
| 122 |
gr.Image(value='logo.png', height=200, width=200, show_label=False)
|
| 123 |
|
| 124 |
+
# Simple chatbot initialization
|
| 125 |
+
chatbot = gr.Chatbot(
|
| 126 |
+
value=[], # Initialize with empty list
|
| 127 |
+
elem_id="chatbot",
|
| 128 |
+
bubble_full_width=False,
|
| 129 |
+
label="Conversation"
|
| 130 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
with gr.Row():
|
| 133 |
query = gr.Textbox(
|
|
|
|
| 146 |
)
|
| 147 |
clear = gr.Button("Clear")
|
| 148 |
|
| 149 |
+
# Fixed event handlers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
submit_btn.click(
|
| 151 |
+
fn=chatbot_func,
|
| 152 |
+
inputs=[query, cross_encoder, chatbot],
|
| 153 |
+
outputs=[query, chatbot]
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
query.submit(
|
| 157 |
+
fn=chatbot_func,
|
| 158 |
+
inputs=[query, cross_encoder, chatbot],
|
| 159 |
+
outputs=[query, chatbot]
|
| 160 |
+
)
|
| 161 |
+
|
| 162 |
+
def clear_conversation():
|
| 163 |
+
return [], ""
|
| 164 |
+
|
| 165 |
+
clear.click(
|
| 166 |
+
fn=clear_conversation,
|
| 167 |
+
inputs=None,
|
| 168 |
+
outputs=[chatbot, query]
|
| 169 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
examples = [
|
| 172 |
'CAN U SAY THE DIFFERENCES BETWEEN METALS AND NON METALS?',
|