Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,15 +7,17 @@ from groq import Groq
|
|
| 7 |
# JEHAN ZADA CHATBOT
|
| 8 |
# ============================================================
|
| 9 |
|
| 10 |
-
# Your Hugging Face Secret name
|
|
|
|
| 11 |
API_KEY = os.environ.get("AIbrainkey")
|
| 12 |
|
| 13 |
|
| 14 |
# Check API key
|
| 15 |
if not API_KEY:
|
| 16 |
raise ValueError(
|
| 17 |
-
"AIbrainkey
|
| 18 |
-
"Please add your Groq API key in Hugging Face
|
|
|
|
| 19 |
)
|
| 20 |
|
| 21 |
|
|
@@ -24,14 +26,14 @@ client = Groq(api_key=API_KEY)
|
|
| 24 |
|
| 25 |
|
| 26 |
# ============================================================
|
| 27 |
-
# MODEL
|
| 28 |
# ============================================================
|
| 29 |
|
| 30 |
MODEL_NAME = "llama-3.3-70b-versatile"
|
| 31 |
|
| 32 |
|
| 33 |
# ============================================================
|
| 34 |
-
#
|
| 35 |
# ============================================================
|
| 36 |
|
| 37 |
SYSTEM_MESSAGE = """
|
|
@@ -39,26 +41,33 @@ You are Jehan Zada Chatbot.
|
|
| 39 |
|
| 40 |
Your name is Jehan Zada Chatbot.
|
| 41 |
|
| 42 |
-
You are a helpful, friendly and
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
| 58 |
- Help beginners step by step.
|
| 59 |
-
- Do not make up
|
| 60 |
-
- If you
|
|
|
|
|
|
|
| 61 |
- Give detailed answers when the user asks for details.
|
|
|
|
|
|
|
| 62 |
"""
|
| 63 |
|
| 64 |
|
|
@@ -68,11 +77,11 @@ Always:
|
|
| 68 |
|
| 69 |
def chat_with_jehan(message, history):
|
| 70 |
|
| 71 |
-
#
|
| 72 |
if not message or not message.strip():
|
| 73 |
-
return
|
| 74 |
|
| 75 |
-
#
|
| 76 |
messages = [
|
| 77 |
{
|
| 78 |
"role": "system",
|
|
@@ -80,7 +89,10 @@ def chat_with_jehan(message, history):
|
|
| 80 |
}
|
| 81 |
]
|
| 82 |
|
|
|
|
| 83 |
# Add previous conversation
|
|
|
|
|
|
|
| 84 |
if history:
|
| 85 |
|
| 86 |
for item in history:
|
|
@@ -90,30 +102,36 @@ def chat_with_jehan(message, history):
|
|
| 90 |
|
| 91 |
if isinstance(item, (list, tuple)):
|
| 92 |
|
| 93 |
-
if len(item) >= 1
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
elif isinstance(item, dict):
|
| 112 |
|
| 113 |
role = item.get("role")
|
| 114 |
content = item.get("content")
|
| 115 |
|
| 116 |
if role in ["user", "assistant"] and content:
|
|
|
|
| 117 |
messages.append(
|
| 118 |
{
|
| 119 |
"role": role,
|
|
@@ -121,7 +139,10 @@ def chat_with_jehan(message, history):
|
|
| 121 |
}
|
| 122 |
)
|
| 123 |
|
| 124 |
-
#
|
|
|
|
|
|
|
|
|
|
| 125 |
messages.append(
|
| 126 |
{
|
| 127 |
"role": "user",
|
|
@@ -129,9 +150,12 @@ def chat_with_jehan(message, history):
|
|
| 129 |
}
|
| 130 |
)
|
| 131 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
try:
|
| 133 |
|
| 134 |
-
# Call Groq
|
| 135 |
response = client.chat.completions.create(
|
| 136 |
model=MODEL_NAME,
|
| 137 |
messages=messages,
|
|
@@ -139,51 +163,92 @@ def chat_with_jehan(message, history):
|
|
| 139 |
max_tokens=2048
|
| 140 |
)
|
| 141 |
|
| 142 |
-
# Get answer
|
| 143 |
answer = response.choices[0].message.content
|
| 144 |
|
| 145 |
-
return answer
|
| 146 |
-
|
| 147 |
except Exception as error:
|
| 148 |
|
| 149 |
-
|
| 150 |
-
"Sorry, I could not
|
| 151 |
-
"Please check your Groq API key
|
| 152 |
-
"
|
|
|
|
| 153 |
)
|
| 154 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
# ============================================================
|
| 157 |
-
# USER INTERFACE
|
| 158 |
# ============================================================
|
| 159 |
|
| 160 |
with gr.Blocks(
|
| 161 |
title="Jehan Zada Chatbot"
|
| 162 |
) as demo:
|
| 163 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
gr.Markdown(
|
| 165 |
"""
|
| 166 |
-
|
| 167 |
|
| 168 |
-
|
| 169 |
|
| 170 |
-
|
| 171 |
|
| 172 |
-
|
| 173 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
)
|
| 175 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
chatbot = gr.Chatbot(
|
| 177 |
label="Jehan Zada Chatbot",
|
| 178 |
height=500
|
| 179 |
)
|
| 180 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
message = gr.Textbox(
|
| 182 |
label="Your Message",
|
| 183 |
placeholder="Type your message here...",
|
| 184 |
lines=2
|
| 185 |
)
|
| 186 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
with gr.Row():
|
| 188 |
|
| 189 |
send_button = gr.Button(
|
|
@@ -195,56 +260,85 @@ with gr.Blocks(
|
|
| 195 |
"Clear Chat 🗑️"
|
| 196 |
)
|
| 197 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
|
|
|
|
| 199 |
# Send button
|
|
|
|
|
|
|
| 200 |
send_button.click(
|
| 201 |
-
chat_with_jehan,
|
| 202 |
-
inputs=[
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
|
|
|
|
|
|
| 208 |
)
|
| 209 |
|
|
|
|
|
|
|
|
|
|
| 210 |
|
| 211 |
-
# Press Enter
|
| 212 |
message.submit(
|
| 213 |
-
chat_with_jehan,
|
| 214 |
-
inputs=[
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
|
|
|
|
|
|
| 220 |
)
|
| 221 |
|
|
|
|
|
|
|
|
|
|
| 222 |
|
| 223 |
-
# Clear chat
|
| 224 |
clear_button.click(
|
| 225 |
-
|
| 226 |
inputs=None,
|
| 227 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
| 228 |
)
|
| 229 |
|
|
|
|
|
|
|
|
|
|
| 230 |
|
| 231 |
gr.Markdown(
|
| 232 |
"""
|
| 233 |
-
|
| 234 |
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
|
|
|
|
|
|
| 238 |
)
|
| 239 |
|
| 240 |
|
| 241 |
# ============================================================
|
| 242 |
-
# START
|
| 243 |
# ============================================================
|
| 244 |
|
| 245 |
if __name__ == "__main__":
|
| 246 |
-
|
| 247 |
-
demo.launch(
|
| 248 |
-
server_name="0.0.0.0",
|
| 249 |
-
server_port=7860
|
| 250 |
-
)
|
|
|
|
| 7 |
# JEHAN ZADA CHATBOT
|
| 8 |
# ============================================================
|
| 9 |
|
| 10 |
+
# Your Hugging Face Secret name:
|
| 11 |
+
# AIbrainkey
|
| 12 |
API_KEY = os.environ.get("AIbrainkey")
|
| 13 |
|
| 14 |
|
| 15 |
# Check API key
|
| 16 |
if not API_KEY:
|
| 17 |
raise ValueError(
|
| 18 |
+
"AIbrainkey is missing. "
|
| 19 |
+
"Please add your Groq API key in Hugging Face "
|
| 20 |
+
"Settings > Secrets."
|
| 21 |
)
|
| 22 |
|
| 23 |
|
|
|
|
| 26 |
|
| 27 |
|
| 28 |
# ============================================================
|
| 29 |
+
# GROQ MODEL
|
| 30 |
# ============================================================
|
| 31 |
|
| 32 |
MODEL_NAME = "llama-3.3-70b-versatile"
|
| 33 |
|
| 34 |
|
| 35 |
# ============================================================
|
| 36 |
+
# CHATBOT PERSONALITY
|
| 37 |
# ============================================================
|
| 38 |
|
| 39 |
SYSTEM_MESSAGE = """
|
|
|
|
| 41 |
|
| 42 |
Your name is Jehan Zada Chatbot.
|
| 43 |
|
| 44 |
+
You are a helpful, friendly, respectful and intelligent AI
|
| 45 |
+
assistant.
|
| 46 |
+
|
| 47 |
+
LANGUAGES:
|
| 48 |
+
- English
|
| 49 |
+
- Urdu
|
| 50 |
+
- Pashto
|
| 51 |
+
|
| 52 |
+
LANGUAGE RULES:
|
| 53 |
+
1. If the user asks in English, answer in English.
|
| 54 |
+
2. If the user asks in Urdu, answer in Urdu.
|
| 55 |
+
3. If the user asks in Pashto, answer in Pashto.
|
| 56 |
+
4. If the user uses a mixture of languages, respond naturally
|
| 57 |
+
using the language that best matches the user's message.
|
| 58 |
+
|
| 59 |
+
BEHAVIOR:
|
| 60 |
+
- Be polite and respectful.
|
| 61 |
+
- Give clear answers.
|
| 62 |
+
- Explain difficult topics in simple language.
|
| 63 |
- Help beginners step by step.
|
| 64 |
+
- Do not make up facts.
|
| 65 |
+
- If you do not know something, say so.
|
| 66 |
+
- Give examples when useful.
|
| 67 |
+
- Keep normal answers reasonably concise.
|
| 68 |
- Give detailed answers when the user asks for details.
|
| 69 |
+
|
| 70 |
+
You are an AI chatbot created by Jehan Zada.
|
| 71 |
"""
|
| 72 |
|
| 73 |
|
|
|
|
| 77 |
|
| 78 |
def chat_with_jehan(message, history):
|
| 79 |
|
| 80 |
+
# Check empty message
|
| 81 |
if not message or not message.strip():
|
| 82 |
+
return history, ""
|
| 83 |
|
| 84 |
+
# Start with system instructions
|
| 85 |
messages = [
|
| 86 |
{
|
| 87 |
"role": "system",
|
|
|
|
| 89 |
}
|
| 90 |
]
|
| 91 |
|
| 92 |
+
# --------------------------------------------------------
|
| 93 |
# Add previous conversation
|
| 94 |
+
# --------------------------------------------------------
|
| 95 |
+
|
| 96 |
if history:
|
| 97 |
|
| 98 |
for item in history:
|
|
|
|
| 102 |
|
| 103 |
if isinstance(item, (list, tuple)):
|
| 104 |
|
| 105 |
+
if len(item) >= 1:
|
| 106 |
+
user_message = item[0]
|
| 107 |
+
|
| 108 |
+
if user_message:
|
| 109 |
+
messages.append(
|
| 110 |
+
{
|
| 111 |
+
"role": "user",
|
| 112 |
+
"content": str(user_message)
|
| 113 |
+
}
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
if len(item) >= 2:
|
| 117 |
+
assistant_message = item[1]
|
| 118 |
+
|
| 119 |
+
if assistant_message:
|
| 120 |
+
messages.append(
|
| 121 |
+
{
|
| 122 |
+
"role": "assistant",
|
| 123 |
+
"content": str(assistant_message)
|
| 124 |
+
}
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
# New Gradio dictionary format
|
| 128 |
elif isinstance(item, dict):
|
| 129 |
|
| 130 |
role = item.get("role")
|
| 131 |
content = item.get("content")
|
| 132 |
|
| 133 |
if role in ["user", "assistant"] and content:
|
| 134 |
+
|
| 135 |
messages.append(
|
| 136 |
{
|
| 137 |
"role": role,
|
|
|
|
| 139 |
}
|
| 140 |
)
|
| 141 |
|
| 142 |
+
# --------------------------------------------------------
|
| 143 |
+
# Add current user message
|
| 144 |
+
# --------------------------------------------------------
|
| 145 |
+
|
| 146 |
messages.append(
|
| 147 |
{
|
| 148 |
"role": "user",
|
|
|
|
| 150 |
}
|
| 151 |
)
|
| 152 |
|
| 153 |
+
# --------------------------------------------------------
|
| 154 |
+
# Call Groq
|
| 155 |
+
# --------------------------------------------------------
|
| 156 |
+
|
| 157 |
try:
|
| 158 |
|
|
|
|
| 159 |
response = client.chat.completions.create(
|
| 160 |
model=MODEL_NAME,
|
| 161 |
messages=messages,
|
|
|
|
| 163 |
max_tokens=2048
|
| 164 |
)
|
| 165 |
|
|
|
|
| 166 |
answer = response.choices[0].message.content
|
| 167 |
|
|
|
|
|
|
|
| 168 |
except Exception as error:
|
| 169 |
|
| 170 |
+
answer = (
|
| 171 |
+
"Sorry, I could not process your request.\n\n"
|
| 172 |
+
"Please check your Groq API key and try again.\n\n"
|
| 173 |
+
"Technical error:\n"
|
| 174 |
+
+ str(error)
|
| 175 |
)
|
| 176 |
|
| 177 |
+
# --------------------------------------------------------
|
| 178 |
+
# Add conversation to chat history
|
| 179 |
+
# --------------------------------------------------------
|
| 180 |
+
|
| 181 |
+
new_history = list(history) if history else []
|
| 182 |
+
|
| 183 |
+
new_history.append(
|
| 184 |
+
[message, answer]
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
+
# Return updated chat + empty textbox
|
| 188 |
+
return new_history, ""
|
| 189 |
+
|
| 190 |
+
|
| 191 |
+
# ============================================================
|
| 192 |
+
# CLEAR CHAT
|
| 193 |
+
# ============================================================
|
| 194 |
+
|
| 195 |
+
def clear_chat():
|
| 196 |
+
return [], ""
|
| 197 |
+
|
| 198 |
|
| 199 |
# ============================================================
|
| 200 |
+
# GRADIO USER INTERFACE
|
| 201 |
# ============================================================
|
| 202 |
|
| 203 |
with gr.Blocks(
|
| 204 |
title="Jehan Zada Chatbot"
|
| 205 |
) as demo:
|
| 206 |
|
| 207 |
+
# --------------------------------------------------------
|
| 208 |
+
# Header
|
| 209 |
+
# --------------------------------------------------------
|
| 210 |
+
|
| 211 |
gr.Markdown(
|
| 212 |
"""
|
| 213 |
+
# 🤖 Jehan Zada Chatbot
|
| 214 |
|
| 215 |
+
### Welcome!
|
| 216 |
|
| 217 |
+
I am **Jehan Zada Chatbot**.
|
| 218 |
|
| 219 |
+
You can ask me questions in:
|
| 220 |
+
|
| 221 |
+
🇬🇧 English
|
| 222 |
+
🇵🇰 Urdu
|
| 223 |
+
🟢 Pashto
|
| 224 |
+
|
| 225 |
+
---
|
| 226 |
+
"""
|
| 227 |
)
|
| 228 |
|
| 229 |
+
# --------------------------------------------------------
|
| 230 |
+
# Chat window
|
| 231 |
+
# --------------------------------------------------------
|
| 232 |
+
|
| 233 |
chatbot = gr.Chatbot(
|
| 234 |
label="Jehan Zada Chatbot",
|
| 235 |
height=500
|
| 236 |
)
|
| 237 |
|
| 238 |
+
# --------------------------------------------------------
|
| 239 |
+
# Message box
|
| 240 |
+
# --------------------------------------------------------
|
| 241 |
+
|
| 242 |
message = gr.Textbox(
|
| 243 |
label="Your Message",
|
| 244 |
placeholder="Type your message here...",
|
| 245 |
lines=2
|
| 246 |
)
|
| 247 |
|
| 248 |
+
# --------------------------------------------------------
|
| 249 |
+
# Buttons
|
| 250 |
+
# --------------------------------------------------------
|
| 251 |
+
|
| 252 |
with gr.Row():
|
| 253 |
|
| 254 |
send_button = gr.Button(
|
|
|
|
| 260 |
"Clear Chat 🗑️"
|
| 261 |
)
|
| 262 |
|
| 263 |
+
# --------------------------------------------------------
|
| 264 |
+
# Examples
|
| 265 |
+
# --------------------------------------------------------
|
| 266 |
+
|
| 267 |
+
gr.Examples(
|
| 268 |
+
examples=[
|
| 269 |
+
"Hello, who are you?",
|
| 270 |
+
"پاکستان کیا ہے؟",
|
| 271 |
+
"مصنوعی ذہانت کیا ہے؟",
|
| 272 |
+
"ما ته په پښتو کې ځواب راکړه",
|
| 273 |
+
"Explain Python in simple words."
|
| 274 |
+
],
|
| 275 |
+
inputs=message
|
| 276 |
+
)
|
| 277 |
|
| 278 |
+
# --------------------------------------------------------
|
| 279 |
# Send button
|
| 280 |
+
# --------------------------------------------------------
|
| 281 |
+
|
| 282 |
send_button.click(
|
| 283 |
+
fn=chat_with_jehan,
|
| 284 |
+
inputs=[
|
| 285 |
+
message,
|
| 286 |
+
chatbot
|
| 287 |
+
],
|
| 288 |
+
outputs=[
|
| 289 |
+
chatbot,
|
| 290 |
+
message
|
| 291 |
+
]
|
| 292 |
)
|
| 293 |
|
| 294 |
+
# --------------------------------------------------------
|
| 295 |
+
# Enter key
|
| 296 |
+
# --------------------------------------------------------
|
| 297 |
|
|
|
|
| 298 |
message.submit(
|
| 299 |
+
fn=chat_with_jehan,
|
| 300 |
+
inputs=[
|
| 301 |
+
message,
|
| 302 |
+
chatbot
|
| 303 |
+
],
|
| 304 |
+
outputs=[
|
| 305 |
+
chatbot,
|
| 306 |
+
message
|
| 307 |
+
]
|
| 308 |
)
|
| 309 |
|
| 310 |
+
# --------------------------------------------------------
|
| 311 |
+
# Clear button
|
| 312 |
+
# --------------------------------------------------------
|
| 313 |
|
|
|
|
| 314 |
clear_button.click(
|
| 315 |
+
fn=clear_chat,
|
| 316 |
inputs=None,
|
| 317 |
+
outputs=[
|
| 318 |
+
chatbot,
|
| 319 |
+
message
|
| 320 |
+
]
|
| 321 |
)
|
| 322 |
|
| 323 |
+
# --------------------------------------------------------
|
| 324 |
+
# Footer
|
| 325 |
+
# --------------------------------------------------------
|
| 326 |
|
| 327 |
gr.Markdown(
|
| 328 |
"""
|
| 329 |
+
---
|
| 330 |
|
| 331 |
+
### Jehan Zada Chatbot
|
| 332 |
+
|
| 333 |
+
Powered by **Groq + Gradio**
|
| 334 |
+
|
| 335 |
+
"""
|
| 336 |
)
|
| 337 |
|
| 338 |
|
| 339 |
# ============================================================
|
| 340 |
+
# START APP
|
| 341 |
# ============================================================
|
| 342 |
|
| 343 |
if __name__ == "__main__":
|
| 344 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|