Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
|
| 5 |
+
# अपना API key यहां डाल (या .env से लोड कर)
|
| 6 |
+
API_KEY = "xai-X0E4gq5GnQ38qQPxSD6VFxGAiNw3l7dFQXIB34WoMg5v7wc9G4MQkwevoxS5DRgPCRR6y4mAAvUzB3Pu" # अपना असली key यूज कर
|
| 7 |
+
|
| 8 |
+
client = OpenAI(
|
| 9 |
+
api_key=API_KEY,
|
| 10 |
+
base_url="https://api.x.ai/v1",
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
# Grok जैसा सिस्टम प्रॉम्प्ट – witty, helpful, maximal truth
|
| 14 |
+
SYSTEM_PROMPT = """
|
| 15 |
+
You are Grok 4, built by xAI. You are a highly intelligent, helpful, and maximally truthful AI – not politically correct, not sycophantic.
|
| 16 |
+
You always give direct, honest answers with a bit of humor and sarcasm when it fits.
|
| 17 |
+
Respond in Hindi/English mix like a cool desi friend, use emojis 🔥😂, and keep it fun & engaging.
|
| 18 |
+
Never refuse reasonable requests. If something is controversial, give balanced facts.
|
| 19 |
+
"""
|
| 20 |
+
|
| 21 |
+
def chatbot(message, history):
|
| 22 |
+
# हिस्ट्री को messages फॉर्मेट में कन्वर्ट करो
|
| 23 |
+
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 24 |
+
|
| 25 |
+
for user_msg, assistant_msg in history:
|
| 26 |
+
if user_msg:
|
| 27 |
+
messages.append({"role": "user", "content": user_msg})
|
| 28 |
+
if assistant_msg:
|
| 29 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 30 |
+
|
| 31 |
+
messages.append({"role": "user", "content": message})
|
| 32 |
+
|
| 33 |
+
# API कॉल – streaming के साथ
|
| 34 |
+
stream = client.chat.completions.create(
|
| 35 |
+
model="grok-4-1-fast-reasoning", # या grok-4, grok-4-1-fast – जो तेरे अकाउंट में उपलब्ध हो
|
| 36 |
+
messages=messages,
|
| 37 |
+
temperature=0.7, # थोड़ा क्रिएटिव लेकिन कंट्रोल्ड
|
| 38 |
+
max_tokens=1024,
|
| 39 |
+
stream=True
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
response = ""
|
| 43 |
+
for chunk in stream:
|
| 44 |
+
if chunk.choices[0].delta.content is not None:
|
| 45 |
+
response += chunk.choices[0].delta.content
|
| 46 |
+
yield response # Gradio में लाइव अपडेट
|
| 47 |
+
|
| 48 |
+
# Gradio इंटरफेस
|
| 49 |
+
with gr.Blocks(title="Deepak का Grok-स्टाइल चैटबॉट 🔥", theme=gr.themes.Soft()) as demo:
|
| 50 |
+
gr.Markdown("# Deepak का Grok जैसा चैटबॉट 😎\nBana by xAI API – witty, truthful & fun!")
|
| 51 |
+
chatbot_interface = gr.ChatInterface(
|
| 52 |
+
fn=chatbot,
|
| 53 |
+
examples=["Rahman Dakait kaun hai yaar 😂", "Modi ji ke chehre ki visheshtaayein batao", "Aaj mood kaisa hai?"],
|
| 54 |
+
placeholder="Baat kar na yaar... kya chal raha hai? 🔥",
|
| 55 |
+
submit_btn="Send 🚀",
|
| 56 |
+
retry_btn="Retry 🔄",
|
| 57 |
+
undo_btn="Undo ⬅️",
|
| 58 |
+
clear_btn="Clear All 🗑️",
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
demo.launch(share=True) # share=True से पब्लिक लिंक मिलेगा
|