Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,33 +7,42 @@ from groq import Groq
|
|
| 7 |
# JEHAN ZADA CHATBOT
|
| 8 |
# ============================================================
|
| 9 |
|
| 10 |
-
#
|
| 11 |
# AIbrainkey
|
|
|
|
| 12 |
API_KEY = os.environ.get("AIbrainkey")
|
| 13 |
|
| 14 |
|
| 15 |
-
#
|
|
|
|
|
|
|
|
|
|
| 16 |
if not API_KEY:
|
| 17 |
raise ValueError(
|
| 18 |
"AIbrainkey is missing. "
|
| 19 |
-
"Please add your Groq API key in
|
| 20 |
-
"Settings > Secrets."
|
| 21 |
)
|
| 22 |
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
|
| 28 |
# ============================================================
|
| 29 |
-
#
|
| 30 |
# ============================================================
|
| 31 |
|
| 32 |
MODEL_NAME = "llama-3.3-70b-versatile"
|
| 33 |
|
| 34 |
|
| 35 |
# ============================================================
|
| 36 |
-
#
|
| 37 |
# ============================================================
|
| 38 |
|
| 39 |
SYSTEM_MESSAGE = """
|
|
@@ -44,25 +53,26 @@ Your name is Jehan Zada Chatbot.
|
|
| 44 |
You are a helpful, friendly, respectful and intelligent AI
|
| 45 |
assistant.
|
| 46 |
|
| 47 |
-
|
| 48 |
- English
|
| 49 |
- Urdu
|
| 50 |
- Pashto
|
| 51 |
|
| 52 |
LANGUAGE RULES:
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
|
| 59 |
BEHAVIOR:
|
|
|
|
| 60 |
- Be polite and respectful.
|
| 61 |
-
- Give clear answers.
|
| 62 |
-
- Explain difficult
|
| 63 |
- Help beginners step by step.
|
| 64 |
-
- Do not
|
| 65 |
-
- If you
|
| 66 |
- Give examples when useful.
|
| 67 |
- Keep normal answers reasonably concise.
|
| 68 |
- Give detailed answers when the user asks for details.
|
|
@@ -77,11 +87,18 @@ You are an AI chatbot created by Jehan Zada.
|
|
| 77 |
|
| 78 |
def chat_with_jehan(message, history):
|
| 79 |
|
|
|
|
| 80 |
# Check empty message
|
|
|
|
|
|
|
| 81 |
if not message or not message.strip():
|
| 82 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
-
# Start with system instructions
|
| 85 |
messages = [
|
| 86 |
{
|
| 87 |
"role": "system",
|
|
@@ -89,6 +106,7 @@ def chat_with_jehan(message, history):
|
|
| 89 |
}
|
| 90 |
]
|
| 91 |
|
|
|
|
| 92 |
# --------------------------------------------------------
|
| 93 |
# Add previous conversation
|
| 94 |
# --------------------------------------------------------
|
|
@@ -97,48 +115,47 @@ def chat_with_jehan(message, history):
|
|
| 97 |
|
| 98 |
for item in history:
|
| 99 |
|
| 100 |
-
#
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
-
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
| 107 |
|
| 108 |
-
if user_message:
|
| 109 |
messages.append(
|
| 110 |
{
|
| 111 |
-
"role":
|
| 112 |
-
"content":
|
| 113 |
}
|
| 114 |
)
|
| 115 |
|
| 116 |
-
if len(item) >= 2:
|
| 117 |
-
assistant_message = item[1]
|
| 118 |
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
{
|
| 122 |
-
"role": "assistant",
|
| 123 |
-
"content": str(assistant_message)
|
| 124 |
-
}
|
| 125 |
-
)
|
| 126 |
|
| 127 |
-
|
| 128 |
-
elif isinstance(item, dict):
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
-
if
|
| 134 |
|
| 135 |
messages.append(
|
| 136 |
{
|
| 137 |
-
"role":
|
| 138 |
-
"content": str(
|
| 139 |
}
|
| 140 |
)
|
| 141 |
|
|
|
|
| 142 |
# --------------------------------------------------------
|
| 143 |
# Add current user message
|
| 144 |
# --------------------------------------------------------
|
|
@@ -150,195 +167,80 @@ def chat_with_jehan(message, history):
|
|
| 150 |
}
|
| 151 |
)
|
| 152 |
|
|
|
|
| 153 |
# --------------------------------------------------------
|
| 154 |
-
#
|
| 155 |
# --------------------------------------------------------
|
| 156 |
|
| 157 |
try:
|
| 158 |
|
| 159 |
response = client.chat.completions.create(
|
|
|
|
| 160 |
model=MODEL_NAME,
|
|
|
|
| 161 |
messages=messages,
|
|
|
|
| 162 |
temperature=0.7,
|
|
|
|
| 163 |
max_tokens=2048
|
| 164 |
)
|
| 165 |
|
|
|
|
|
|
|
| 166 |
answer = response.choices[0].message.content
|
| 167 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
except Exception as error:
|
| 169 |
|
| 170 |
-
|
| 171 |
-
"Sorry, I could not process your request.\n\n"
|
| 172 |
-
"Please check your Groq API key
|
| 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 |
-
#
|
| 193 |
# ============================================================
|
| 194 |
|
| 195 |
-
|
| 196 |
-
return [], ""
|
| 197 |
-
|
| 198 |
|
| 199 |
-
|
| 200 |
-
# GRADIO USER INTERFACE
|
| 201 |
-
# ============================================================
|
| 202 |
|
| 203 |
-
|
| 204 |
-
title="Jehan Zada Chatbot"
|
| 205 |
-
) as demo:
|
| 206 |
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
# --------------------------------------------------------
|
| 210 |
|
| 211 |
-
|
| 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 |
-
|
| 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 |
-
|
| 253 |
-
|
| 254 |
-
send_button = gr.Button(
|
| 255 |
-
"Send 🚀",
|
| 256 |
-
variant="primary"
|
| 257 |
-
)
|
| 258 |
-
|
| 259 |
-
clear_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
|
| 341 |
# ============================================================
|
| 342 |
|
| 343 |
if __name__ == "__main__":
|
|
|
|
| 344 |
demo.launch()
|
|
|
|
| 7 |
# JEHAN ZADA CHATBOT
|
| 8 |
# ============================================================
|
| 9 |
|
| 10 |
+
# Hugging Face Secret name:
|
| 11 |
# AIbrainkey
|
| 12 |
+
|
| 13 |
API_KEY = os.environ.get("AIbrainkey")
|
| 14 |
|
| 15 |
|
| 16 |
+
# ============================================================
|
| 17 |
+
# CHECK API KEY
|
| 18 |
+
# ============================================================
|
| 19 |
+
|
| 20 |
if not API_KEY:
|
| 21 |
raise ValueError(
|
| 22 |
"AIbrainkey is missing. "
|
| 23 |
+
"Please add your Groq API key in "
|
| 24 |
+
"Hugging Face > Settings > Secrets."
|
| 25 |
)
|
| 26 |
|
| 27 |
|
| 28 |
+
# ============================================================
|
| 29 |
+
# GROQ CLIENT
|
| 30 |
+
# ============================================================
|
| 31 |
+
|
| 32 |
+
client = Groq(
|
| 33 |
+
api_key=API_KEY
|
| 34 |
+
)
|
| 35 |
|
| 36 |
|
| 37 |
# ============================================================
|
| 38 |
+
# MODEL
|
| 39 |
# ============================================================
|
| 40 |
|
| 41 |
MODEL_NAME = "llama-3.3-70b-versatile"
|
| 42 |
|
| 43 |
|
| 44 |
# ============================================================
|
| 45 |
+
# SYSTEM PROMPT
|
| 46 |
# ============================================================
|
| 47 |
|
| 48 |
SYSTEM_MESSAGE = """
|
|
|
|
| 53 |
You are a helpful, friendly, respectful and intelligent AI
|
| 54 |
assistant.
|
| 55 |
|
| 56 |
+
You can communicate in:
|
| 57 |
- English
|
| 58 |
- Urdu
|
| 59 |
- Pashto
|
| 60 |
|
| 61 |
LANGUAGE RULES:
|
| 62 |
+
|
| 63 |
+
1. If the user writes in English, answer in English.
|
| 64 |
+
2. If the user writes in Urdu, answer in Urdu.
|
| 65 |
+
3. If the user writes in Pashto, answer in Pashto.
|
| 66 |
+
4. If the user mixes languages, respond naturally.
|
| 67 |
|
| 68 |
BEHAVIOR:
|
| 69 |
+
|
| 70 |
- Be polite and respectful.
|
| 71 |
+
- Give clear and useful answers.
|
| 72 |
+
- Explain difficult subjects in simple language.
|
| 73 |
- Help beginners step by step.
|
| 74 |
+
- Do not invent facts.
|
| 75 |
+
- If you are unsure, clearly say that you are unsure.
|
| 76 |
- Give examples when useful.
|
| 77 |
- Keep normal answers reasonably concise.
|
| 78 |
- Give detailed answers when the user asks for details.
|
|
|
|
| 87 |
|
| 88 |
def chat_with_jehan(message, history):
|
| 89 |
|
| 90 |
+
# --------------------------------------------------------
|
| 91 |
# Check empty message
|
| 92 |
+
# --------------------------------------------------------
|
| 93 |
+
|
| 94 |
if not message or not message.strip():
|
| 95 |
+
return "Please write a message first."
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
# --------------------------------------------------------
|
| 99 |
+
# Create messages for Groq
|
| 100 |
+
# --------------------------------------------------------
|
| 101 |
|
|
|
|
| 102 |
messages = [
|
| 103 |
{
|
| 104 |
"role": "system",
|
|
|
|
| 106 |
}
|
| 107 |
]
|
| 108 |
|
| 109 |
+
|
| 110 |
# --------------------------------------------------------
|
| 111 |
# Add previous conversation
|
| 112 |
# --------------------------------------------------------
|
|
|
|
| 115 |
|
| 116 |
for item in history:
|
| 117 |
|
| 118 |
+
# Dictionary format
|
| 119 |
+
if isinstance(item, dict):
|
| 120 |
+
|
| 121 |
+
role = item.get("role")
|
| 122 |
+
content = item.get("content")
|
| 123 |
|
| 124 |
+
if role in ["user", "assistant"]:
|
| 125 |
|
| 126 |
+
# Simple text
|
| 127 |
+
if isinstance(content, str):
|
| 128 |
|
|
|
|
| 129 |
messages.append(
|
| 130 |
{
|
| 131 |
+
"role": role,
|
| 132 |
+
"content": content
|
| 133 |
}
|
| 134 |
)
|
| 135 |
|
|
|
|
|
|
|
| 136 |
|
| 137 |
+
# Old tuple/list format
|
| 138 |
+
elif isinstance(item, (list, tuple)):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
|
| 140 |
+
if len(item) >= 1 and item[0]:
|
|
|
|
| 141 |
|
| 142 |
+
messages.append(
|
| 143 |
+
{
|
| 144 |
+
"role": "user",
|
| 145 |
+
"content": str(item[0])
|
| 146 |
+
}
|
| 147 |
+
)
|
| 148 |
|
| 149 |
+
if len(item) >= 2 and item[1]:
|
| 150 |
|
| 151 |
messages.append(
|
| 152 |
{
|
| 153 |
+
"role": "assistant",
|
| 154 |
+
"content": str(item[1])
|
| 155 |
}
|
| 156 |
)
|
| 157 |
|
| 158 |
+
|
| 159 |
# --------------------------------------------------------
|
| 160 |
# Add current user message
|
| 161 |
# --------------------------------------------------------
|
|
|
|
| 167 |
}
|
| 168 |
)
|
| 169 |
|
| 170 |
+
|
| 171 |
# --------------------------------------------------------
|
| 172 |
+
# Ask Groq
|
| 173 |
# --------------------------------------------------------
|
| 174 |
|
| 175 |
try:
|
| 176 |
|
| 177 |
response = client.chat.completions.create(
|
| 178 |
+
|
| 179 |
model=MODEL_NAME,
|
| 180 |
+
|
| 181 |
messages=messages,
|
| 182 |
+
|
| 183 |
temperature=0.7,
|
| 184 |
+
|
| 185 |
max_tokens=2048
|
| 186 |
)
|
| 187 |
|
| 188 |
+
|
| 189 |
+
# Get answer
|
| 190 |
answer = response.choices[0].message.content
|
| 191 |
|
| 192 |
+
|
| 193 |
+
return answer
|
| 194 |
+
|
| 195 |
+
|
| 196 |
except Exception as error:
|
| 197 |
|
| 198 |
+
return (
|
| 199 |
+
"❌ Sorry, I could not process your request.\n\n"
|
| 200 |
+
"Please check your Groq API key.\n\n"
|
| 201 |
"Technical error:\n"
|
| 202 |
+ str(error)
|
| 203 |
)
|
| 204 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
|
| 206 |
# ============================================================
|
| 207 |
+
# GRADIO CHAT INTERFACE
|
| 208 |
# ============================================================
|
| 209 |
|
| 210 |
+
demo = gr.ChatInterface(
|
|
|
|
|
|
|
| 211 |
|
| 212 |
+
fn=chat_with_jehan,
|
|
|
|
|
|
|
| 213 |
|
| 214 |
+
title="🤖 Jehan Zada Chatbot",
|
|
|
|
|
|
|
| 215 |
|
| 216 |
+
description="""
|
| 217 |
+
Welcome to **Jehan Zada Chatbot**!
|
|
|
|
| 218 |
|
| 219 |
+
You can ask questions in:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
|
| 221 |
🇬🇧 English
|
| 222 |
🇵🇰 Urdu
|
| 223 |
🟢 Pashto
|
| 224 |
+
""",
|
| 225 |
|
| 226 |
+
examples=[
|
| 227 |
+
"Hello, who are you?",
|
| 228 |
+
"پاکستان کیا ہے؟",
|
| 229 |
+
"مصنوعی ذہانت کیا ہے؟",
|
| 230 |
+
"ما ته په پښتو کې ځواب راکړه",
|
| 231 |
+
"Explain Python in simple words."
|
| 232 |
+
],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
|
| 234 |
+
submit_btn="Send 🚀",
|
|
|
|
|
|
|
| 235 |
|
| 236 |
+
clear_btn="Clear Chat 🗑️"
|
| 237 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
|
| 239 |
|
| 240 |
# ============================================================
|
| 241 |
+
# START
|
| 242 |
# ============================================================
|
| 243 |
|
| 244 |
if __name__ == "__main__":
|
| 245 |
+
|
| 246 |
demo.launch()
|