Spaces:
Sleeping
Sleeping
File size: 2,817 Bytes
e5c88fa a50fa0d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | """
AI Chatbot with Gradio - Lesson 2
Build and Share AI Demos Instantly with Gradio ๐
A self-contained chatbot app using LangChain + Gradio.
Perfect for demos, job interviews, and portfolio projects.
"""
import gradio as gr
from langchain_groq import ChatGroq
from langchain_core.messages import HumanMessage, SystemMessage
import os
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Initialize the LLM
llm = ChatGroq(
model="llama-3.1-8b-instant",
temperature=0.7,
api_key=os.getenv("GROQ_API_KEY")
)
def get_ai_response(user_message: str) -> str:
"""Generate AI response for user message"""
messages = [
SystemMessage(
content="You are a helpful AI assistant. Answer the user's questions."
),
HumanMessage(content=user_message),
]
response = llm.invoke(messages)
return response.content
def chatbot_interface(message, history):
"""Handle chatbot conversation flow"""
if not message.strip():
return history, ""
bot_response = get_ai_response(message)
# Add to history using the new messages format
history.append({"role": "user", "content": message})
history.append({"role": "assistant", "content": bot_response})
return history, ""
def create_interface():
"""Create and configure the Gradio interface"""
with gr.Blocks(theme=gr.themes.Soft()) as demo:
# Header
gr.Markdown("# ๐ค AI Chatbot Assistant")
gr.Markdown("Ask me anything and I'll help you with intelligent responses!")
# Chat components
chatbot = gr.Chatbot(
value=[],
height=400,
label="Chat History",
show_copy_button=True,
type="messages" # Use new messages format
)
msg = gr.Textbox(
placeholder="Type your message here...",
label="Your Message",
lines=2
)
# Buttons
with gr.Row():
submit_btn = gr.Button("Send", variant="primary")
clear_btn = gr.Button("Clear Chat", variant="secondary")
# Event handlers
submit_btn.click(
chatbot_interface,
inputs=[msg, chatbot],
outputs=[chatbot, msg]
)
msg.submit(
chatbot_interface,
inputs=[msg, chatbot],
outputs=[chatbot, msg]
)
clear_btn.click(
lambda: ([], ""),
inputs=[],
outputs=[chatbot, msg]
)
# Footer
gr.Markdown("---")
gr.Markdown("๐ก Built with Gradio + LangChain + Groq")
return demo
if __name__ == "__main__":
# Create and launch the interface
demo = create_interface()
demo.launch() |