Spaces:
Sleeping
Sleeping
mayar-waleed commited on
Commit ·
9847e53
1
Parent(s): d4dab2b
Add application file
Browse files
app.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
from app.deps import get_chain
|
| 4 |
+
|
| 5 |
+
def chat_fn(message, history):
|
| 6 |
+
"""
|
| 7 |
+
history comes as: [(user_msg, bot_msg), ...]
|
| 8 |
+
We'll convert it to your chain's expected format:
|
| 9 |
+
[{"role":"user","content":...}, {"role":"assistant","content":...}, ...]
|
| 10 |
+
"""
|
| 11 |
+
history_dicts = []
|
| 12 |
+
for u, a in history:
|
| 13 |
+
if u:
|
| 14 |
+
history_dicts.append({"role": "user", "content": u})
|
| 15 |
+
if a:
|
| 16 |
+
history_dicts.append({"role": "assistant", "content": a})
|
| 17 |
+
|
| 18 |
+
chain = get_chain(conversation_history=history_dicts)
|
| 19 |
+
|
| 20 |
+
# Your chain.invoke is sync
|
| 21 |
+
result = chain.invoke(message)
|
| 22 |
+
answer = result.get("answer", "") if isinstance(result, dict) else str(result)
|
| 23 |
+
return answer
|
| 24 |
+
|
| 25 |
+
demo = gr.ChatInterface(
|
| 26 |
+
fn=chat_fn,
|
| 27 |
+
title="Legal RAG Chatbot",
|
| 28 |
+
description="Ask questions and get answers with retrieved context."
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
if __name__ == "__main__":
|
| 32 |
+
demo.launch()
|