rahulparajuli commited on
Commit
e5c88fa
·
1 Parent(s): 14a8642

first commit

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