Spaces:
Running
Running
Download app.py from jehanzada/Samplechatboot: direct link, hf CLI and curl.
- Browser
- Download file 6.68 kB
-
https://huggingface.co/spaces/jehanzada/Samplechatboot/resolve/main/app.py
- Command line
-
hf download hf://spaces/jehanzada/Samplechatboot/app.py
-
curl -L -o app.py https://huggingface.co/spaces/jehanzada/Samplechatboot/resolve/main/app.py
6.68 kB
| 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() |