Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,42 +7,63 @@ from groq import Groq
|
|
| 7 |
# JEHAN ZADA CHATBOT
|
| 8 |
# ============================================================
|
| 9 |
|
| 10 |
-
# Get Groq API key from Hugging Face
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
|
|
|
| 15 |
raise ValueError(
|
| 16 |
-
"
|
| 17 |
-
"Please add your Groq API key in
|
|
|
|
| 18 |
)
|
| 19 |
|
| 20 |
|
| 21 |
# Create Groq client
|
| 22 |
-
client = Groq(api_key=
|
| 23 |
|
| 24 |
|
| 25 |
# ============================================================
|
| 26 |
-
#
|
| 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 |
-
|
| 38 |
-
-
|
| 39 |
-
-
|
| 40 |
-
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
-
|
| 44 |
-
-
|
| 45 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
"""
|
| 47 |
|
| 48 |
|
|
@@ -52,14 +73,15 @@ Rules:
|
|
| 52 |
|
| 53 |
def chat_with_jehan(message, history):
|
| 54 |
"""
|
| 55 |
-
Send the user's message
|
|
|
|
| 56 |
"""
|
| 57 |
|
| 58 |
-
#
|
| 59 |
if not message or not message.strip():
|
| 60 |
return "Please write a message first."
|
| 61 |
|
| 62 |
-
# Start
|
| 63 |
messages = [
|
| 64 |
{
|
| 65 |
"role": "system",
|
|
@@ -68,25 +90,26 @@ def chat_with_jehan(message, history):
|
|
| 68 |
]
|
| 69 |
|
| 70 |
# Add previous conversation
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
| 90 |
messages.append(
|
| 91 |
{
|
| 92 |
"role": "user",
|
|
@@ -96,7 +119,7 @@ def chat_with_jehan(message, history):
|
|
| 96 |
|
| 97 |
try:
|
| 98 |
|
| 99 |
-
# Send
|
| 100 |
response = client.chat.completions.create(
|
| 101 |
model=MODEL_NAME,
|
| 102 |
messages=messages,
|
|
@@ -104,100 +127,61 @@ def chat_with_jehan(message, history):
|
|
| 104 |
max_tokens=2048
|
| 105 |
)
|
| 106 |
|
| 107 |
-
# Get AI
|
| 108 |
answer = response.choices[0].message.content
|
| 109 |
|
| 110 |
return answer
|
| 111 |
|
| 112 |
except Exception as error:
|
| 113 |
|
|
|
|
| 114 |
return (
|
| 115 |
-
"Sorry,
|
| 116 |
-
|
|
|
|
| 117 |
)
|
| 118 |
|
| 119 |
|
| 120 |
# ============================================================
|
| 121 |
-
# GRADIO
|
| 122 |
# ============================================================
|
| 123 |
|
| 124 |
-
|
| 125 |
-
|
| 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 |
-
|
| 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 |
-
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 |
# ============================================================
|
|
@@ -205,4 +189,8 @@ with gr.Blocks(
|
|
| 205 |
# ============================================================
|
| 206 |
|
| 207 |
if __name__ == "__main__":
|
| 208 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# JEHAN ZADA CHATBOT
|
| 8 |
# ============================================================
|
| 9 |
|
| 10 |
+
# Get Groq API key from Hugging Face Secret
|
| 11 |
+
# IMPORTANT:
|
| 12 |
+
# Your Hugging Face Secret name must be: AIbrainkey
|
| 13 |
+
API_KEY = os.environ.get("AIbrainkey")
|
| 14 |
|
| 15 |
+
|
| 16 |
+
# Check API key
|
| 17 |
+
if not API_KEY:
|
| 18 |
raise ValueError(
|
| 19 |
+
"AIbrainkey was not found. "
|
| 20 |
+
"Please add your Groq API key in "
|
| 21 |
+
"Hugging Face > Settings > Secrets."
|
| 22 |
)
|
| 23 |
|
| 24 |
|
| 25 |
# Create Groq client
|
| 26 |
+
client = Groq(api_key=API_KEY)
|
| 27 |
|
| 28 |
|
| 29 |
# ============================================================
|
| 30 |
+
# AI MODEL
|
| 31 |
# ============================================================
|
| 32 |
|
| 33 |
MODEL_NAME = "llama-3.3-70b-versatile"
|
| 34 |
|
| 35 |
|
| 36 |
+
# ============================================================
|
| 37 |
+
# JEHAN ZADA CHATBOT PERSONALITY
|
| 38 |
+
# ============================================================
|
| 39 |
+
|
| 40 |
SYSTEM_MESSAGE = """
|
| 41 |
You are Jehan Zada Chatbot, a helpful and friendly AI assistant.
|
| 42 |
|
| 43 |
Your name is Jehan Zada Chatbot.
|
| 44 |
|
| 45 |
+
You can communicate in:
|
| 46 |
+
- English
|
| 47 |
+
- Urdu
|
| 48 |
+
- Pashto
|
| 49 |
+
|
| 50 |
+
Language rule:
|
| 51 |
+
- If the user writes in English, answer in English.
|
| 52 |
+
- If the user writes in Urdu, answer in Urdu.
|
| 53 |
+
- If the user writes in Pashto, answer in Pashto.
|
| 54 |
+
- If the user mixes languages, respond naturally in the language that is most appropriate.
|
| 55 |
+
|
| 56 |
+
Your behavior:
|
| 57 |
+
- Be helpful, polite, respectful and friendly.
|
| 58 |
+
- Explain difficult things in simple language.
|
| 59 |
+
- When a beginner asks a technical question, explain it step by step.
|
| 60 |
+
- Do not make up facts.
|
| 61 |
+
- If you are uncertain, clearly say that you are uncertain.
|
| 62 |
+
- Keep normal answers reasonably concise.
|
| 63 |
+
- Give detailed answers when the user asks for details.
|
| 64 |
+
- Use headings and bullet points when they make the answer easier to understand.
|
| 65 |
+
|
| 66 |
+
You are an AI chatbot created by Jehan Zada.
|
| 67 |
"""
|
| 68 |
|
| 69 |
|
|
|
|
| 73 |
|
| 74 |
def chat_with_jehan(message, history):
|
| 75 |
"""
|
| 76 |
+
Send the user's message and previous conversation
|
| 77 |
+
to Groq and return the AI response.
|
| 78 |
"""
|
| 79 |
|
| 80 |
+
# Check empty message
|
| 81 |
if not message or not message.strip():
|
| 82 |
return "Please write a message first."
|
| 83 |
|
| 84 |
+
# Start messages with system instructions
|
| 85 |
messages = [
|
| 86 |
{
|
| 87 |
"role": "system",
|
|
|
|
| 90 |
]
|
| 91 |
|
| 92 |
# Add previous conversation
|
| 93 |
+
if history:
|
| 94 |
+
for item in history:
|
| 95 |
+
|
| 96 |
+
# Get role
|
| 97 |
+
role = item.get("role")
|
| 98 |
+
|
| 99 |
+
# Get content
|
| 100 |
+
content = item.get("content")
|
| 101 |
+
|
| 102 |
+
# We only need normal text messages
|
| 103 |
+
if role in ["user", "assistant"] and isinstance(content, str):
|
| 104 |
+
|
| 105 |
+
messages.append(
|
| 106 |
+
{
|
| 107 |
+
"role": role,
|
| 108 |
+
"content": content
|
| 109 |
+
}
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
# Add the new user message
|
| 113 |
messages.append(
|
| 114 |
{
|
| 115 |
"role": "user",
|
|
|
|
| 119 |
|
| 120 |
try:
|
| 121 |
|
| 122 |
+
# Send conversation to Groq
|
| 123 |
response = client.chat.completions.create(
|
| 124 |
model=MODEL_NAME,
|
| 125 |
messages=messages,
|
|
|
|
| 127 |
max_tokens=2048
|
| 128 |
)
|
| 129 |
|
| 130 |
+
# Get AI answer
|
| 131 |
answer = response.choices[0].message.content
|
| 132 |
|
| 133 |
return answer
|
| 134 |
|
| 135 |
except Exception as error:
|
| 136 |
|
| 137 |
+
# Show a simple error message
|
| 138 |
return (
|
| 139 |
+
"Sorry, I could not process your request.\n\n"
|
| 140 |
+
"Please check your Groq API key and try again.\n\n"
|
| 141 |
+
f"Technical error: {str(error)}"
|
| 142 |
)
|
| 143 |
|
| 144 |
|
| 145 |
# ============================================================
|
| 146 |
+
# GRADIO CHATBOT
|
| 147 |
# ============================================================
|
| 148 |
|
| 149 |
+
demo = gr.ChatInterface(
|
| 150 |
+
fn=chat_with_jehan,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
+
title="🤖 Jehan Zada Chatbot",
|
| 153 |
|
| 154 |
+
description=(
|
| 155 |
+
"Welcome to Jehan Zada Chatbot! "
|
| 156 |
+
"You can ask questions in English, Urdu, or Pashto."
|
| 157 |
+
),
|
| 158 |
|
| 159 |
+
chatbot=gr.Chatbot(
|
| 160 |
+
type="messages",
|
| 161 |
+
height=550,
|
| 162 |
+
placeholder=(
|
| 163 |
+
"<h2>🤖 Jehan Zada Chatbot</h2>"
|
| 164 |
+
"<p>Ask me anything...</p>"
|
| 165 |
)
|
| 166 |
+
),
|
| 167 |
|
| 168 |
+
textbox=gr.Textbox(
|
| 169 |
+
placeholder="Type your message here...",
|
| 170 |
+
container=True,
|
| 171 |
+
scale=7
|
| 172 |
+
),
|
| 173 |
+
|
| 174 |
+
submit_btn="Send 🚀",
|
| 175 |
+
stop_btn="Stop",
|
| 176 |
+
clear_btn="Clear Chat 🗑️",
|
| 177 |
+
|
| 178 |
+
examples=[
|
| 179 |
+
"Hello, who are you?",
|
| 180 |
+
"پاکستان کیا ہے؟",
|
| 181 |
+
"ما ته په پښتو کې ځواب راکړه",
|
| 182 |
+
"Explain artificial intelligence in simple words."
|
| 183 |
+
]
|
| 184 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
|
| 186 |
|
| 187 |
# ============================================================
|
|
|
|
| 189 |
# ============================================================
|
| 190 |
|
| 191 |
if __name__ == "__main__":
|
| 192 |
+
|
| 193 |
+
demo.launch(
|
| 194 |
+
server_name="0.0.0.0",
|
| 195 |
+
server_port=7860
|
| 196 |
+
)
|