jehanzada commited on
Commit
e6b2cfa
·
verified ·
1 Parent(s): 82d6e57

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -117
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 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
 
@@ -52,14 +73,15 @@ Rules:
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",
@@ -68,25 +90,26 @@ def chat_with_jehan(message, history):
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",
@@ -96,7 +119,7 @@ def chat_with_jehan(message, history):
96
 
97
  try:
98
 
99
- # Send request to Groq
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 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
  # ============================================================
@@ -205,4 +189,8 @@ with gr.Blocks(
205
  # ============================================================
206
 
207
  if __name__ == "__main__":
208
- demo.launch()
 
 
 
 
 
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
+ )