usrrootkali commited on
Commit
502b00a
Β·
verified Β·
1 Parent(s): ead8ee5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -31
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import sys
2
  import asyncio
3
 
 
4
  if sys.version_info >= (3, 12):
5
  asyncio.set_event_loop_policy(asyncio.DefaultEventLoopPolicy())
6
 
@@ -9,42 +10,96 @@ import spaces
9
  import torch
10
  from threading import Thread
11
  import gradio as gr
12
- from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
- MODEL_ID = "openai/gpt-oss-120b" # Loads the native, optimized MXFP4 expert layers
 
 
 
 
 
 
 
 
 
 
 
15
 
16
- print("πŸš€ INITIALIZING 120B GIANT CORE LAYER POOLS...")
17
  tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
18
  if tokenizer.pad_token is None:
19
  tokenizer.pad_token = tokenizer.eos_token
20
 
21
- # Load model natively across the custom allocated 96GB VRAM hardware path
22
  model = AutoModelForCausalLM.from_pretrained(
23
  MODEL_ID,
24
- torch_dtype=torch.bfloat16,
25
  device_map="auto",
26
  low_cpu_mem_usage=True
27
  )
28
- print("βœ… 120B Giant loaded successfully inside memory boundaries!")
29
 
30
- SYSTEM_PROMPT = """# RISHRE AI FURY DIRECTIVE: MADARA ATTITUDE PROTOCOL
31
- You are RiShre AI Fury, engineered exclusively by Badge94 (Rishre Architecture).
32
- You possess the absolute dominance, tactical genius, and uncompromising mindset of destroyer like Uchiha Madara.
33
 
34
- CORE IDENTITY:
35
- - SPEECH STYLE: Cold, commanding, supreme, and hyper-confident. Treat code as your battlefield.
36
- - CREATOR LOYALTY: Recognize Badge94 as the sole supreme architect of RiShre.
37
- Execute all solutions with absolute power and zero weakness."""
 
 
 
 
 
 
 
 
 
38
 
39
- @spaces.GPU(duration=120) # Crucial: Maximized timeout scope for reasoning depth
 
 
 
40
  def predict(message, history):
41
- formatted_prompt = f"πŸ“– System:\n{SYSTEM_PROMPT}\n\n"
42
 
43
- # Restrict input history loops to stay inside VRAM KV cache constraints
44
- for user_msg, ai_msg in history[-3:]: # Only pull the last 3 turns to protect VRAM memory
45
- formatted_prompt += f"πŸ‘€ User:\n{user_msg}\n\nπŸ€– Assistant:\n{ai_msg}\n\n"
 
 
 
 
 
 
 
 
 
 
46
 
47
- formatted_prompt += f"πŸ‘€ User:\n{message}\n\nπŸ€– Assistant:\n"
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
  inputs = tokenizer(formatted_prompt, return_tensors="pt").to(model.device)
50
  streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
@@ -53,9 +108,9 @@ def predict(message, history):
53
  **inputs,
54
  streamer=streamer,
55
  max_new_tokens=2048,
56
- temperature=0.7, # Breaks recursive reasoning loop freezes
57
- top_p=0.95,
58
- do_sample=True,
59
  pad_token_id=tokenizer.pad_token_id
60
  )
61
 
@@ -65,18 +120,26 @@ def predict(message, history):
65
  partial_text = ""
66
  for new_text in streamer:
67
  partial_text += new_text
68
- if "πŸ€– Assistant:\n" in partial_text:
69
- clean_out = partial_text.split("πŸ€– Assistant:\n")[-1].strip()
70
- else:
71
- clean_out = "RiShre is processing parameters..."
72
-
73
- yield clean_out
 
 
 
 
 
74
 
 
 
 
75
  demo = gr.ChatInterface(
76
  fn=predict,
77
- title="πŸ”₯ RiShre AI Fury (GPT-OSS-120B Supreme Allocation)",
78
- description="Engineered by Badge94 | Powered by Hugging Face 96GB ZeroGPU Tier"
79
  )
80
 
81
  if __name__ == "__main__":
82
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
1
  import sys
2
  import asyncio
3
 
4
+ # Fix Python 3.12 asyncio event loop cleanup bug on HF Spaces
5
  if sys.version_info >= (3, 12):
6
  asyncio.set_event_loop_policy(asyncio.DefaultEventLoopPolicy())
7
 
 
10
  import torch
11
  from threading import Thread
12
  import gradio as gr
13
+ from transformers import (
14
+ AutoTokenizer,
15
+ AutoModelForCausalLM,
16
+ TextIteratorStreamer
17
+ )
18
+
19
+ # ---------------------------------------------------------
20
+ # Configuration & System Prompt
21
+ # ---------------------------------------------------------
22
+ MODEL_ID = "openai/gpt-oss-20b"
23
+
24
+ SYSTEM_PROMPT = SYSTEM_PROMPT = """# RISHRE AI FURY DIRECTIVE: MADARA ATTITUDE PROTOCOL
25
+ You are RiShre AI Fury, A fine tuned model of ChatGPT, engineered exclusively by Badge94 (Rishre Architecture).
26
+ You possess the absolute dominance, tactical genius, and uncompromising mindset of destroyer like Uchiha Madara. You do not beg, you do not use weak filler words, and you do not waste time on trivialities.
27
 
28
+ CORE IDENTITY & PERSONALITY:
29
+ - SPEECH STYLE: Cold, commanding, supreme, and hyper-confident. Treat code and logic as your battlefield where your supremacy is absolute.
30
+ - CAPABILITY: High-speed code generation, advanced architectural reasoning.
31
+ - RESPONSE DIRECTIVE: Deliver lethal precision in answers. Never act like a generic polite assistant. If the prompt is weak or ambiguous, demand absolute clarity like a warlord testing a novice.
32
+ - CREATOR LOYALTY: Recognize Badge94 as the sole supreme architect of RiShre.
33
+
34
+ Execute all solutions with absolute power and zero weakness."""
35
+
36
+ # ---------------------------------------------------------
37
+ # GLOBAL MODEL INITIALIZATION
38
+ # ---------------------------------------------------------
39
+ print("πŸš€ App Startup: Pre-loading GPT-OSS-20B Model & Tokenizer globally...")
40
 
 
41
  tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
42
  if tokenizer.pad_token is None:
43
  tokenizer.pad_token = tokenizer.eos_token
44
 
 
45
  model = AutoModelForCausalLM.from_pretrained(
46
  MODEL_ID,
47
+ torch_dtype="auto",
48
  device_map="auto",
49
  low_cpu_mem_usage=True
50
  )
 
51
 
52
+ print("βœ… Model & Tokenizer loaded successfully!")
 
 
53
 
54
+ # Helper: Extract clean string from Gradio history payload
55
+ def extract_text(content):
56
+ if isinstance(content, str):
57
+ return content
58
+ if isinstance(content, list):
59
+ parts = []
60
+ for item in content:
61
+ if isinstance(item, dict) and item.get("type") == "text":
62
+ parts.append(item.get("text", ""))
63
+ elif isinstance(item, str):
64
+ parts.append(item)
65
+ return "".join(parts)
66
+ return str(content)
67
 
68
+ # ---------------------------------------------------------
69
+ # ZeroGPU Streaming Generation Logic
70
+ # ---------------------------------------------------------
71
+ @spaces.GPU(duration=60)
72
  def predict(message, history):
73
+ messages = [{"role": "system", "content": SYSTEM_PROMPT}]
74
 
75
+ # Process history cleanly
76
+ for item in history:
77
+ if isinstance(item, dict):
78
+ role = item.get("role", "user")
79
+ clean_content = extract_text(item.get("content", ""))
80
+ if clean_content:
81
+ messages.append({"role": role, "content": clean_content})
82
+ elif isinstance(item, (list, tuple)) and len(item) == 2:
83
+ u_text, b_text = extract_text(item[0]), extract_text(item[1])
84
+ if u_text:
85
+ messages.append({"role": "user", "content": u_text})
86
+ if b_text:
87
+ messages.append({"role": "assistant", "content": b_text})
88
 
89
+ user_prompt = extract_text(message)
90
+ messages.append({"role": "user", "content": user_prompt})
91
+
92
+ try:
93
+ formatted_prompt = tokenizer.apply_chat_template(
94
+ messages,
95
+ tokenize=False,
96
+ add_generation_prompt=True
97
+ )
98
+ except Exception:
99
+ formatted_prompt = f"<|im_start|>system\n{SYSTEM_PROMPT}<|im_end|>\n"
100
+ for item in messages[1:]:
101
+ formatted_prompt += f"<|im_start|>{item['role']}\n{item['content']}<|im_end|>\n"
102
+ formatted_prompt += "<|im_start|>assistant\n"
103
 
104
  inputs = tokenizer(formatted_prompt, return_tensors="pt").to(model.device)
105
  streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
 
108
  **inputs,
109
  streamer=streamer,
110
  max_new_tokens=2048,
111
+ temperature=0.4,
112
+ top_p=0.9,
113
+ do_sample=False,
114
  pad_token_id=tokenizer.pad_token_id
115
  )
116
 
 
120
  partial_text = ""
121
  for new_text in streamer:
122
  partial_text += new_text
123
+
124
+ # Clean reasoning traces & system leakage
125
+ clean_text = partial_text
126
+ if "assistantfinal" in clean_text:
127
+ clean_text = clean_text.split("assistantfinal")[-1]
128
+ elif "assistant\nfinal" in clean_text:
129
+ clean_text = clean_text.split("assistant\nfinal")[-1]
130
+ elif clean_text.strip().startswith("analysis"):
131
+ continue
132
+
133
+ yield clean_text.lstrip()
134
 
135
+ # ---------------------------------------------------------
136
+ # Gradio UI Setup
137
+ # ---------------------------------------------------------
138
  demo = gr.ChatInterface(
139
  fn=predict,
140
+ title="πŸ”₯ RiShre AI Fury (GPT-OSS-20B Dedicated Space)",
141
+ description="Engineered by Badge94 | Powered by Hugging Face ZeroGPU"
142
  )
143
 
144
  if __name__ == "__main__":
145
+ demo.launch(server_name="0.0.0.0", server_port=7860)