Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from answer import answer_question
|
| 3 |
|
|
|
|
| 4 |
# ---------- Helper: format retrieved chunks ----------
|
| 5 |
def format_chunks(chunks):
|
| 6 |
if not chunks:
|
|
@@ -22,26 +23,28 @@ def format_chunks(chunks):
|
|
| 22 |
|
| 23 |
# ---------- Chat handler (tuple-based, HF compatible) ----------
|
| 24 |
def chat_handler(message, chat_history):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
chat_history = chat_history or []
|
| 26 |
|
| 27 |
-
# Build history
|
| 28 |
history_text = ""
|
| 29 |
-
for
|
| 30 |
-
history_text += f"{
|
| 31 |
|
| 32 |
# Call RAG backend
|
| 33 |
answer, chunks = answer_question(message, history_text)
|
| 34 |
|
| 35 |
-
# Append
|
| 36 |
-
chat_history.append(
|
| 37 |
-
chat_history.append({"role": "assistant", "content": answer})
|
| 38 |
|
| 39 |
chunk_display = format_chunks(chunks)
|
| 40 |
|
| 41 |
return chat_history, chunk_display, ""
|
| 42 |
|
| 43 |
|
| 44 |
-
|
| 45 |
# ---------- Gradio UI ----------
|
| 46 |
with gr.Blocks(title="BlinkNow β DSA RAG Assistant") as demo:
|
| 47 |
|
|
@@ -54,11 +57,11 @@ with gr.Blocks(title="BlinkNow β DSA RAG Assistant") as demo:
|
|
| 54 |
)
|
| 55 |
|
| 56 |
with gr.Row():
|
|
|
|
| 57 |
with gr.Column(scale=2):
|
| 58 |
chatbot = gr.Chatbot(
|
| 59 |
label="Chat",
|
| 60 |
-
height=500
|
| 61 |
-
type="messages"
|
| 62 |
)
|
| 63 |
|
| 64 |
with gr.Row():
|
|
@@ -69,12 +72,14 @@ with gr.Blocks(title="BlinkNow β DSA RAG Assistant") as demo:
|
|
| 69 |
)
|
| 70 |
send_btn = gr.Button("Send", variant="primary", scale=1)
|
| 71 |
|
|
|
|
| 72 |
with gr.Column(scale=3):
|
| 73 |
gr.Markdown("### π Retrieved Context")
|
| 74 |
retrieved_chunks = gr.Markdown(
|
| 75 |
value="*Retrieved knowledge chunks will appear here after your query.*"
|
| 76 |
)
|
| 77 |
|
|
|
|
| 78 |
send_btn.click(
|
| 79 |
fn=chat_handler,
|
| 80 |
inputs=[user_input, chatbot],
|
|
@@ -87,5 +92,6 @@ with gr.Blocks(title="BlinkNow β DSA RAG Assistant") as demo:
|
|
| 87 |
outputs=[chatbot, retrieved_chunks, user_input]
|
| 88 |
)
|
| 89 |
|
|
|
|
| 90 |
if __name__ == "__main__":
|
| 91 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from answer import answer_question
|
| 3 |
|
| 4 |
+
|
| 5 |
# ---------- Helper: format retrieved chunks ----------
|
| 6 |
def format_chunks(chunks):
|
| 7 |
if not chunks:
|
|
|
|
| 23 |
|
| 24 |
# ---------- Chat handler (tuple-based, HF compatible) ----------
|
| 25 |
def chat_handler(message, chat_history):
|
| 26 |
+
"""
|
| 27 |
+
message: str
|
| 28 |
+
chat_history: List[Tuple[user, assistant]]
|
| 29 |
+
"""
|
| 30 |
chat_history = chat_history or []
|
| 31 |
|
| 32 |
+
# Build history string for RAG backend
|
| 33 |
history_text = ""
|
| 34 |
+
for user_msg, bot_msg in chat_history:
|
| 35 |
+
history_text += f"User: {user_msg}\nAssistant: {bot_msg}\n"
|
| 36 |
|
| 37 |
# Call RAG backend
|
| 38 |
answer, chunks = answer_question(message, history_text)
|
| 39 |
|
| 40 |
+
# Append new turn (TUPLE FORMAT)
|
| 41 |
+
chat_history.append((message, answer))
|
|
|
|
| 42 |
|
| 43 |
chunk_display = format_chunks(chunks)
|
| 44 |
|
| 45 |
return chat_history, chunk_display, ""
|
| 46 |
|
| 47 |
|
|
|
|
| 48 |
# ---------- Gradio UI ----------
|
| 49 |
with gr.Blocks(title="BlinkNow β DSA RAG Assistant") as demo:
|
| 50 |
|
|
|
|
| 57 |
)
|
| 58 |
|
| 59 |
with gr.Row():
|
| 60 |
+
# Left: Chat
|
| 61 |
with gr.Column(scale=2):
|
| 62 |
chatbot = gr.Chatbot(
|
| 63 |
label="Chat",
|
| 64 |
+
height=500
|
|
|
|
| 65 |
)
|
| 66 |
|
| 67 |
with gr.Row():
|
|
|
|
| 72 |
)
|
| 73 |
send_btn = gr.Button("Send", variant="primary", scale=1)
|
| 74 |
|
| 75 |
+
# Right: Retrieved Context
|
| 76 |
with gr.Column(scale=3):
|
| 77 |
gr.Markdown("### π Retrieved Context")
|
| 78 |
retrieved_chunks = gr.Markdown(
|
| 79 |
value="*Retrieved knowledge chunks will appear here after your query.*"
|
| 80 |
)
|
| 81 |
|
| 82 |
+
# ---------- Events ----------
|
| 83 |
send_btn.click(
|
| 84 |
fn=chat_handler,
|
| 85 |
inputs=[user_input, chatbot],
|
|
|
|
| 92 |
outputs=[chatbot, retrieved_chunks, user_input]
|
| 93 |
)
|
| 94 |
|
| 95 |
+
|
| 96 |
if __name__ == "__main__":
|
| 97 |
demo.launch()
|