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