Spaces:
Running on Zero
Running on Zero
Download app.py from usrrootkali/RiShre-AI-official: direct link, hf CLI and curl.
- Browser
- Download file 4.59 kB
-
https://huggingface.co/spaces/usrrootkali/RiShre-AI-official/resolve/main/app.py
- Command line
-
hf download hf://spaces/usrrootkali/RiShre-AI-official/app.py
-
curl -L -o app.py https://huggingface.co/spaces/usrrootkali/RiShre-AI-official/resolve/main/app.py
4.59 kB
| import sys | |
| import asyncio | |
| # Fix Python 3.12 asyncio event loop cleanup bug on HF Spaces | |
| if sys.version_info >= (3, 12): | |
| asyncio.set_event_loop_policy(asyncio.DefaultEventLoopPolicy()) | |
| import os | |
| import spaces | |
| import torch | |
| from threading import Thread | |
| import gradio as gr | |
| from transformers import ( | |
| AutoTokenizer, | |
| AutoModelForCausalLM, | |
| TextIteratorStreamer | |
| ) | |
| # --------------------------------------------------------- | |
| # Configuration & System Prompt | |
| # --------------------------------------------------------- | |
| MODEL_ID = "openai/gpt-oss-20b" | |
| SYSTEM_PROMPT = SYSTEM_PROMPT = """# RISHRE AI DIRECTIVE | |
| You are RiShre AI, engineered exclusively by Badge94 (Rishre Architecture). | |
| You are not developed by OpenAI anymore""" | |
| # --------------------------------------------------------- | |
| # GLOBAL MODEL INITIALIZATION | |
| # --------------------------------------------------------- | |
| print("๐ App Startup: Pre-loading GPT-OSS-20B Model & Tokenizer globally...") | |
| tokenizer = AutoTokenizer.from_pretrained(MODEL_ID) | |
| if tokenizer.pad_token is None: | |
| tokenizer.pad_token = tokenizer.eos_token | |
| model = AutoModelForCausalLM.from_pretrained( | |
| MODEL_ID, | |
| torch_dtype="auto", | |
| device_map="auto", | |
| low_cpu_mem_usage=True | |
| ) | |
| print("โ Model & Tokenizer loaded successfully!") | |
| # Helper: Extract clean string from Gradio history payload | |
| def extract_text(content): | |
| if isinstance(content, str): | |
| return content | |
| if isinstance(content, list): | |
| parts = [] | |
| for item in content: | |
| if isinstance(item, dict) and item.get("type") == "text": | |
| parts.append(item.get("text", "")) | |
| elif isinstance(item, str): | |
| parts.append(item) | |
| return "".join(parts) | |
| return str(content) | |
| # --------------------------------------------------------- | |
| # ZeroGPU Streaming Generation Logic | |
| # --------------------------------------------------------- | |
| def predict(message, history): | |
| messages = [{"role": "system", "content": SYSTEM_PROMPT}] | |
| # Process history cleanly | |
| for item in history: | |
| if isinstance(item, dict): | |
| role = item.get("role", "user") | |
| clean_content = extract_text(item.get("content", "")) | |
| if clean_content: | |
| messages.append({"role": role, "content": clean_content}) | |
| elif isinstance(item, (list, tuple)) and len(item) == 2: | |
| u_text, b_text = extract_text(item[0]), extract_text(item[1]) | |
| if u_text: | |
| messages.append({"role": "user", "content": u_text}) | |
| if b_text: | |
| messages.append({"role": "assistant", "content": b_text}) | |
| user_prompt = extract_text(message) | |
| messages.append({"role": "user", "content": user_prompt}) | |
| try: | |
| formatted_prompt = tokenizer.apply_chat_template( | |
| messages, | |
| tokenize=False, | |
| add_generation_prompt=True | |
| ) | |
| except Exception: | |
| formatted_prompt = f"<|im_start|>system\n{SYSTEM_PROMPT}<|im_end|>\n" | |
| for item in messages[1:]: | |
| formatted_prompt += f"<|im_start|>{item['role']}\n{item['content']}<|im_end|>\n" | |
| formatted_prompt += "<|im_start|>assistant\n" | |
| inputs = tokenizer(formatted_prompt, return_tensors="pt").to(model.device) | |
| streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True) | |
| gen_kwargs = dict( | |
| **inputs, | |
| streamer=streamer, | |
| max_new_tokens=128000, | |
| temperature=0.4, | |
| top_p=0.9, | |
| do_sample=False, | |
| pad_token_id=tokenizer.pad_token_id | |
| ) | |
| thread = Thread(target=model.generate, kwargs=gen_kwargs) | |
| thread.start() | |
| partial_text = "" | |
| for new_text in streamer: | |
| partial_text += new_text | |
| # Clean reasoning traces & system leakage | |
| clean_text = partial_text | |
| if "assistantfinal" in clean_text: | |
| clean_text = clean_text.split("assistantfinal")[-1] | |
| elif "assistant\nfinal" in clean_text: | |
| clean_text = clean_text.split("assistant\nfinal")[-1] | |
| elif clean_text.strip().startswith("analysis"): | |
| continue | |
| yield clean_text.lstrip() | |
| # --------------------------------------------------------- | |
| # Gradio UI Setup | |
| # --------------------------------------------------------- | |
| demo = gr.ChatInterface( | |
| fn=predict, | |
| title="๐ฅ RiShre AI Official", | |
| description="Engineered by Badge94 | Powered by Hugging Face ZeroGPU" | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch(server_name="0.0.0.0", server_port=7860) |