| import os |
| import gradio as gr |
| from openai import OpenAI |
|
|
| |
| API_KEY = "xai-X0E4gq5GnQ38qQPxSD6VFxGAiNw3l7dFQXIB34WoMg5v7wc9G4MQkwevoxS5DRgPCRR6y4mAAvUzB3Pu" |
|
|
| client = OpenAI( |
| api_key=API_KEY, |
| base_url="https://api.x.ai/v1", |
| ) |
|
|
| SYSTEM_PROMPT = """ |
| You are Grok 4, built by xAI. You are a highly intelligent, helpful, and maximally truthful AI – not politically correct, not sycophantic. |
| You always give direct, honest answers with a bit of humor and sarcasm when it fits. |
| Respond in Hindi/English mix like a cool desi friend, use emojis 🔥😂, and keep it fun & engaging. |
| Never refuse reasonable requests. If something is controversial, give balanced facts. |
| """ |
|
|
| def chatbot(message, history): |
| messages = [{"role": "system", "content": SYSTEM_PROMPT}] |
| |
| for user_msg, assistant_msg in history: |
| if user_msg: |
| messages.append({"role": "user", "content": user_msg}) |
| if assistant_msg: |
| messages.append({"role": "assistant", "content": assistant_msg}) |
| |
| messages.append({"role": "user", "content": message}) |
| |
| stream = client.chat.completions.create( |
| model="grok-4-1-fast-reasoning", |
| messages=messages, |
| temperature=0.7, |
| max_tokens=1024, |
| stream=True |
| ) |
| |
| response = "" |
| for chunk in stream: |
| if chunk.choices[0].delta.content is not None: |
| response += chunk.choices[0].delta.content |
| yield response |
|
|
| |
| custom_textbox = gr.Textbox( |
| placeholder="Baat kar na yaar... kya chal raha hai? 🔥", |
| container=False, |
| scale=7, |
| lines=1, |
| autofocus=True |
| ) |
|
|
| |
| with gr.Blocks(title="Deepak का Grok-स्टाइल चैटबॉट 🔥") as demo: |
| gr.Markdown("# Deepak का Grok जैसा चैटबॉट 😎\nBana by xAI API – witty, truthful & fun!") |
| |
| gr.ChatInterface( |
| fn=chatbot, |
| examples=["Rahman Dakait kaun hai yaar 😂", "Modi ji ke chehre ki visheshtaayein batao", "Aaj mood kaisa hai?"], |
| textbox=custom_textbox, |
| submit_btn="Send 🚀", |
| retry_btn="Retry 🔄", |
| undo_btn="Undo ⬅️", |
| clear_btn="Clear All 🗑️", |
| ) |
|
|
| |
| demo.launch( |
| theme=gr.themes.Soft(), |
| share=True |
| ) |