Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
from utils.chatbot_logic import create_chatbot_chain
|
| 4 |
+
|
| 5 |
+
# Initialize the chatbot chain
|
| 6 |
+
chatbot_chain = create_chatbot_chain()
|
| 7 |
+
|
| 8 |
+
def chat_interface(message, history):
|
| 9 |
+
"""
|
| 10 |
+
Process user message and return chatbot response
|
| 11 |
+
"""
|
| 12 |
+
try:
|
| 13 |
+
response = chatbot_chain.invoke({"input": message})
|
| 14 |
+
return response["output"]
|
| 15 |
+
except Exception as e:
|
| 16 |
+
return f"Error: {str(e)}"
|
| 17 |
+
|
| 18 |
+
# Create Gradio Chat Interface
|
| 19 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 20 |
+
gr.Markdown(
|
| 21 |
+
"""
|
| 22 |
+
# 🤖 LangChain Runnables Chatbot
|
| 23 |
+
|
| 24 |
+
A modular chatbot built using **LangChain Runnables** with multiple response strategies.
|
| 25 |
+
|
| 26 |
+
**Features:**
|
| 27 |
+
- Fact retrieval
|
| 28 |
+
- Joke generation
|
| 29 |
+
- Conversational memory
|
| 30 |
+
- Modular and extensible design
|
| 31 |
+
"""
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
chatbot = gr.Chatbot(
|
| 35 |
+
label="Chat History",
|
| 36 |
+
height=400,
|
| 37 |
+
type="messages"
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
msg = gr.Textbox(
|
| 41 |
+
label="Your Message",
|
| 42 |
+
placeholder="Ask me anything...",
|
| 43 |
+
lines=2
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
clear = gr.Button("Clear Chat")
|
| 47 |
+
|
| 48 |
+
def respond(message, chat_history):
|
| 49 |
+
bot_message = chat_interface(message, chat_history)
|
| 50 |
+
chat_history.append({"role": "user", "content": message})
|
| 51 |
+
chat_history.append({"role": "assistant", "content": bot_message})
|
| 52 |
+
return "", chat_history
|
| 53 |
+
|
| 54 |
+
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 55 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 56 |
+
|
| 57 |
+
if __name__ == "__main__":
|
| 58 |
+
demo.launch()
|