jehanzada commited on
Commit
82d6e57
ยท
verified ยท
1 Parent(s): a833b59

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +198 -20
app.py CHANGED
@@ -1,30 +1,208 @@
1
  import os
2
- import streamlit as st
3
  from groq import Groq
4
 
5
- st.set_page_config(page_title="Sample Chatbot", page_icon="๐Ÿค–")
6
 
7
- st.title("๐Ÿค– Jehan Zada Chatboot")
 
 
8
 
9
- # Read API key
10
- api_key = os.getenv("Samplechatboot")
11
 
12
- if not api_key:
13
- st.error("API key not found. Please add Samplechatboot in Space secrets.")
14
- st.stop()
 
 
 
15
 
16
- client = Groq(api_key=api_key)
17
 
18
- user_input = st.text_input("Ask your question:")
 
19
 
20
- if st.button("Send"):
21
- if user_input.strip():
22
- with st.spinner("Thinking..."):
23
- response = client.chat.completions.create(
24
- model="llama-3.3-70b-versatile",
25
- messages=[{"role": "user", "content": user_input}],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  )
27
- st.success("Response:")
28
- st.write(response.choices[0].message.content)
29
- else:
30
- st.warning("Please enter a message.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
+ import gradio as gr
3
  from groq import Groq
4
 
 
5
 
6
+ # ============================================================
7
+ # JEHAN ZADA CHATBOT
8
+ # ============================================================
9
 
10
+ # Get Groq API key from Hugging Face Secrets
11
+ GROQ_API_KEY = os.environ.get("AIbrainkey")
12
 
13
+ # Check if API key exists
14
+ if not GROQ_API_KEY:
15
+ raise ValueError(
16
+ "GROQ_API_KEY is missing. "
17
+ "Please add your Groq API key in Hugging Face Secrets."
18
+ )
19
 
 
20
 
21
+ # Create Groq client
22
+ client = Groq(api_key=GROQ_API_KEY)
23
 
24
+
25
+ # ============================================================
26
+ # CHATBOT SETTINGS
27
+ # ============================================================
28
+
29
+ MODEL_NAME = "llama-3.3-70b-versatile"
30
+
31
+
32
+ SYSTEM_MESSAGE = """
33
+ You are Jehan Zada Chatbot, a helpful and friendly AI assistant.
34
+
35
+ Your name is Jehan Zada Chatbot.
36
+
37
+ Rules:
38
+ - Be helpful, polite and respectful.
39
+ - Give clear and easy-to-understand answers.
40
+ - If the user asks in Urdu, answer in Urdu.
41
+ - If the user asks in English, answer in English.
42
+ - If the user asks in Pashto, answer in Pashto when possible.
43
+ - For difficult topics, explain things step by step.
44
+ - Do not pretend to know something if you are not sure.
45
+ - Keep answers reasonably concise unless the user asks for details.
46
+ """
47
+
48
+
49
+ # ============================================================
50
+ # CHAT FUNCTION
51
+ # ============================================================
52
+
53
+ def chat_with_jehan(message, history):
54
+ """
55
+ Send the user's message to Groq and return the AI response.
56
+ """
57
+
58
+ # If user sends an empty message
59
+ if not message or not message.strip():
60
+ return "Please write a message first."
61
+
62
+ # Start conversation with system instructions
63
+ messages = [
64
+ {
65
+ "role": "system",
66
+ "content": SYSTEM_MESSAGE
67
+ }
68
+ ]
69
+
70
+ # Add previous conversation
71
+ for user_message, assistant_message in history:
72
+
73
+ if user_message:
74
+ messages.append(
75
+ {
76
+ "role": "user",
77
+ "content": user_message
78
+ }
79
+ )
80
+
81
+ if assistant_message:
82
+ messages.append(
83
+ {
84
+ "role": "assistant",
85
+ "content": assistant_message
86
+ }
87
  )
88
+
89
+ # Add current user message
90
+ messages.append(
91
+ {
92
+ "role": "user",
93
+ "content": message
94
+ }
95
+ )
96
+
97
+ try:
98
+
99
+ # Send request to Groq
100
+ response = client.chat.completions.create(
101
+ model=MODEL_NAME,
102
+ messages=messages,
103
+ temperature=0.7,
104
+ max_tokens=2048
105
+ )
106
+
107
+ # Get AI response
108
+ answer = response.choices[0].message.content
109
+
110
+ return answer
111
+
112
+ except Exception as error:
113
+
114
+ return (
115
+ "Sorry, something went wrong.\n\n"
116
+ f"Error: {str(error)}"
117
+ )
118
+
119
+
120
+ # ============================================================
121
+ # GRADIO INTERFACE
122
+ # ============================================================
123
+
124
+ with gr.Blocks(
125
+ title="Jehan Zada Chatbot"
126
+ ) as demo:
127
+
128
+ gr.Markdown(
129
+ """
130
+ # ๐Ÿค– Jehan Zada Chatbot
131
+
132
+ ### Welcome! I am Jehan Zada Chatbot.
133
+
134
+ Ask me anything in **English, Urdu, or Pashto**.
135
+
136
+ ---
137
+ """
138
+ )
139
+
140
+ chatbot = gr.Chatbot(
141
+ label="Jehan Zada Chatbot",
142
+ height=500
143
+ )
144
+
145
+ message = gr.Textbox(
146
+ label="Your Message",
147
+ placeholder="Type your message here...",
148
+ lines=2
149
+ )
150
+
151
+ with gr.Row():
152
+
153
+ send_button = gr.Button(
154
+ "Send ๐Ÿš€",
155
+ variant="primary"
156
+ )
157
+
158
+ clear_button = gr.Button(
159
+ "Clear Chat ๐Ÿ—‘๏ธ"
160
+ )
161
+
162
+
163
+ # Send message when button is clicked
164
+ send_button.click(
165
+ chat_with_jehan,
166
+ inputs=[message, chatbot],
167
+ outputs=chatbot
168
+ ).then(
169
+ lambda: "",
170
+ inputs=None,
171
+ outputs=message
172
+ )
173
+
174
+
175
+ # Send message when Enter is pressed
176
+ message.submit(
177
+ chat_with_jehan,
178
+ inputs=[message, chatbot],
179
+ outputs=chatbot
180
+ ).then(
181
+ lambda: "",
182
+ inputs=None,
183
+ outputs=message
184
+ )
185
+
186
+
187
+ # Clear conversation
188
+ clear_button.click(
189
+ lambda: [],
190
+ inputs=None,
191
+ outputs=chatbot
192
+ )
193
+
194
+
195
+ gr.Markdown(
196
+ """
197
+ ---
198
+ **Jehan Zada Chatbot** | Powered by Groq + Gradio
199
+ """
200
+ )
201
+
202
+
203
+ # ============================================================
204
+ # START APPLICATION
205
+ # ============================================================
206
+
207
+ if __name__ == "__main__":
208
+ demo.launch()