mohitk1234 commited on
Commit
c8a3e31
·
verified ·
1 Parent(s): d0a1a38

yes for pm chat bot

Browse files
Files changed (1) hide show
  1. app.py +114 -0
app.py ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+ import gradio as gr
4
+
5
+ # Groq API Configuration
6
+ groq_api_key = os.getenv("GROQ_API_KEY")
7
+ url = "https://api.groq.com/openai/v1/chat/completions"
8
+ headers = {
9
+ "Authorization": f"Bearer {groq_api_key}",
10
+ "Content-Type": "application/json"
11
+ }
12
+
13
+ # Assistant role
14
+ system_role = "You are a Product Manager and you will answer questions as a Product Manager."
15
+ user_db = {"admin@example.com": "password123"} # Simulated user DB
16
+
17
+ # Auth logic
18
+ def authenticate(email, password, mode):
19
+ if mode == "Login":
20
+ if email in user_db and user_db[email] == password:
21
+ return gr.update(visible=True), gr.update(visible=False), "", "✅ Login successful"
22
+ else:
23
+ return gr.update(visible=False), gr.update(visible=True), "❌ Invalid email or password", ""
24
+ elif mode == "Sign Up":
25
+ if email in user_db:
26
+ return gr.update(visible=False), gr.update(visible=True), "❌ Email already exists", ""
27
+ user_db[email] = password
28
+ return gr.update(visible=True), gr.update(visible=False), "", "✅ Account created"
29
+
30
+ # Chat logic
31
+ def chat(user_input, history):
32
+ messages = [{"role": "system", "content": system_role}]
33
+ for user_msg, assistant_msg in history:
34
+ messages.append({"role": "user", "content": user_msg})
35
+ messages.append({"role": "assistant", "content": assistant_msg})
36
+ messages.append({"role": "user", "content": user_input})
37
+
38
+ body = {
39
+ "model": "meta-llama/llama-4-maverick-17b-128e-instruct",
40
+ "messages": messages
41
+ }
42
+
43
+ response = requests.post(url, headers=headers, json=body)
44
+ if response.status_code == 200:
45
+ reply = response.json()['choices'][0]['message']['content']
46
+ history.append((user_input, reply))
47
+ return history, history
48
+ else:
49
+ error = f"❌ Error: {response.status_code} - {response.text}"
50
+ history.append((user_input, error))
51
+ return history, history
52
+
53
+ # Reset logic
54
+ def reset():
55
+ return [], []
56
+
57
+ # Toggle password visibility
58
+ def toggle_password(view):
59
+ return gr.update(type="text" if view else "password")
60
+
61
+ # Gradio UI
62
+ with gr.Blocks(css="""
63
+ #chat-area {
64
+ height: 500px;
65
+ overflow-y: auto;
66
+ background: #F9FAFB;
67
+ border: 1px solid #D1D5DB;
68
+ border-radius: 8px;
69
+ padding: 10px;
70
+ }
71
+ #send-chat, #reset-chat, #auth-btn {
72
+ background-color: #2563EB !important;
73
+ color: white !important;
74
+ border-radius: 6px;
75
+ padding: 10px 16px;
76
+ font-weight: 600;
77
+ }
78
+ #send-chat:hover, #reset-chat:hover, #auth-btn:hover {
79
+ background-color: #1D4ED8 !important;
80
+ }
81
+ """) as demo:
82
+
83
+ # Title
84
+ gr.Markdown("## 💬 Product Manager Chatbot")
85
+
86
+ # Login/Signup section
87
+ with gr.Column(visible=True) as login_section:
88
+ gr.Markdown("### 🔐 Login or Sign Up to Continue")
89
+ email = gr.Textbox(label="Email", placeholder="you@example.com")
90
+ password = gr.Textbox(label="Password", type="password")
91
+ show_password = gr.Checkbox(label="Show password")
92
+ show_password.change(fn=toggle_password, inputs=show_password, outputs=password)
93
+ mode = gr.Radio(["Login", "Sign Up"], value="Login", label="Mode")
94
+ auth_btn = gr.Button("Submit", elem_id="auth-btn")
95
+ auth_error = gr.Textbox(visible=True, interactive=False, show_label=False)
96
+ auth_success = gr.Textbox(visible=True, interactive=False, show_label=False)
97
+
98
+ # Chat section
99
+ with gr.Column(visible=False) as chat_section:
100
+ chatbot = gr.Chatbot(label="Chat", elem_id="chat-area")
101
+ with gr.Row():
102
+ user_input = gr.Textbox(show_label=False, placeholder="Ask a product question...", lines=1, scale=8)
103
+ send_btn = gr.Button("Send", elem_id="send-chat", scale=1)
104
+ reset_btn = gr.Button("Reset Chat", elem_id="reset-chat")
105
+ state = gr.State([])
106
+
107
+ # Button logic
108
+ auth_btn.click(fn=authenticate, inputs=[email, password, mode], outputs=[chat_section, login_section, auth_error, auth_success])
109
+ send_btn.click(fn=chat, inputs=[user_input, state], outputs=[chatbot, state])
110
+ user_input.submit(fn=chat, inputs=[user_input, state], outputs=[chatbot, state])
111
+ reset_btn.click(fn=reset, outputs=[chatbot, state])
112
+
113
+ # Launch app
114
+ demo.launch()