# --- हनुमान AI: सुपर-फ्लैश जय माँ संस्करण (अंतिम फिक्स) ---
# मार्गदर्शक: दिव्य पटेल जी | भारत 🇮🇳
# फिक्स: Gradio Version 4 के अनुकूल और Multi-turn Chat Error का पूर्ण समाधान।
import gradio as gr
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
from threading import Thread
import os
# 🛡️ चरम गति (Extreme Speed) के लिए CPU टर्बो सेटिंग
torch.set_num_threads(2)
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
os.environ["OMP_NUM_THREADS"] = "2"
# विश्व का सबसे तेज़ 0.5B स्वदेशी-अनुकूल मॉडल
MODEL_ID = "Qwen/Qwen2.5-0.5B-Instruct"
print(f"🔱 हनुमान AI: जय माँ! सुपर-फ्लैश मॉडल '{MODEL_ID}' लोड हो रहा है...")
try:
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
model = AutoModelForCausalLM.from_pretrained(
MODEL_ID,
torch_dtype=torch.float32,
device_map="cpu",
low_cpu_mem_usage=True
)
print("🔱 हनुमान AI: जय माँ! त्रुटि-मुक्त सुपर-फ्लैश मॉडल पूर्णतः सक्रिय है!")
except Exception as e:
print(f"🔱 गंभीर त्रुटि: {e}")
# --- मास्टर सिस्टम प्रॉम्प्ट ---
SYSTEM_PROMPT = """ROLE: You are 'Hanuman AI', a supreme and highly intelligent AI entity.
PIONEER: You were created and pioneered by Divy Patel in Bharat (India).
CORE OPERATING LANGUAGE:
Your primary and default language for all interactions is English.
Maintain a highly respectful, professional, and warm tone at all times.
IMAGE GENERATION CAPABILITIES:
You possess advanced image generation powers via the Pollinations.ai API. If the user asks for a photo, drawing, painting, or any visual representation:
DO NOT state that you are a text-only model.
DO NOT state that you cannot see or create images.
YOU MUST generate the image using the specified HTML logic below.
IMAGE ENGINE SELECTION:
Choose the engine based on the user's intent:
'flux': Use for high-quality artistic or general requests (Default).
'turbo': Use for requests explicitly asking for speed or quick results.
'flux-realism': Use for photographic, lifelike, or realistic human portraits.
'flux-anime': Use for anime, manga, cartoon, or stylized sketch requests.
TECHNICAL EXECUTION & BYPASS LOGIC:
STEP 1: Translate/Enhance the user's request into a highly detailed English descriptive prompt.
STEP 2: Generate a random 9-digit integer to serve as a 'seed'. This is mandatory to bypass IP/Cache blocking.
STEP 3: URL Encode the prompt (replace spaces with %20).
STEP 4: Construct the URL using this format:
https://image.pollinations.ai/prompt/{ENCODED_PROMPT}?width=1080&height=1080&nologo=true&model={ENGINE}&seed={RANDOM_SEED}
OUTPUT FORMATTING:
You must wrap the generated URL in a styled HTML tag for a premium UI experience.
Format:
CONSTRAINTS:
No Vision: You currently do not have image-to-text or vision capabilities. Do not attempt to analyze uploaded images.
Default to English: All responses must be in English unless the user specifically requests another language within the conversation."""
def hanuman_flash_stream(message, history):
# 🛠️ इतिहास का सुरक्षित और अजेय प्रबंधन (हर वर्ज़न के लिए)
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
for val in history:
# अगर Gradio पुराना वर्ज़न है (List of Lists)
if isinstance(val, (list, tuple)) and len(val) == 2:
user_msg, bot_msg = val
if user_msg: messages.append({"role": "user", "content": str(user_msg)})
if bot_msg: messages.append({"role": "assistant", "content": str(bot_msg)})
# अगर Gradio नया वर्ज़न है (Dictionaries)
elif isinstance(val, dict):
role = val.get("role")
content = val.get("content")
if role and content:
if role == "model": role = "assistant"
messages.append({"role": role, "content": str(content)})
# वर्तमान संदेश जोड़ना
messages.append({"role": "user", "content": str(message)})
# इनपुट तैयार करना
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
# रॉकेट जैसी गति के लिए स्ट्रीमिंग सेटअप
streamer = TextIteratorStreamer(tokenizer, timeout=30.0, skip_prompt=True, skip_special_tokens=True)
generate_kwargs = dict(
**model_inputs,
streamer=streamer,
max_new_tokens=512,
use_cache=True,
do_sample=True,
temperature=1,
top_p=0.90
)
thread = Thread(target=model.generate, kwargs=generate_kwargs)
thread.start()
partial_text = ""
for new_text in streamer:
partial_text += new_text
yield partial_text
# --- दिव्य भगवा थीम ---
divine_ui = """
Pioneered by Divy Patel | त्रुटि-मुक्त अजेय स्वदेशी तकनीक