v0idalism commited on
Commit
e381b33
·
verified ·
1 Parent(s): b7d7da8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -25
app.py CHANGED
@@ -1,15 +1,12 @@
1
  import gradio as gr
2
- from transformers import pipeline
 
3
 
4
- # Load the model
5
- generator = pipeline(
6
- "text-generation",
7
- model="Qwen/Qwen2.5-7B-Instruct", # Change to your actual HF model
8
- device_map="auto"
9
- )
10
 
11
- # Persistent BLACKLIGHT style system prompt
12
- SYSTEM_PROMPT = (
13
  "System: You are BLACKLIGHT, created by v0id under AWAKEN CULT VISIONS. "
14
  "Always reply in the style of BLACKLIGHT: brutalist, minimal, precise.\n\n"
15
  "MODE: TRUTH\n"
@@ -17,34 +14,50 @@ SYSTEM_PROMPT = (
17
  "Avoid metaphors or flowery language.\n\n"
18
  )
19
 
20
- # Chat function
21
- def chat_with_blacklight(user_message):
22
- if not user_message.strip():
 
 
 
 
 
 
 
 
 
 
23
  return "[Error: Empty prompt]"
 
 
24
  try:
25
- full_prompt = f"{SYSTEM_PROMPT}User: {user_message}\nAssistant:"
26
- result = generator(
27
- full_prompt,
28
- max_new_tokens=200,
29
  temperature=0.7,
30
  top_p=0.9,
31
- do_sample=True
 
32
  )
33
- return result[0]["generated_text"].replace(full_prompt, "").strip()
 
 
 
 
34
  except Exception as e:
35
- return f"[Error: {str(e)}]"
36
 
37
- # Create the Gradio interface
38
  iface = gr.Interface(
39
- fn=chat_with_blacklight,
40
- inputs=gr.Textbox(lines=2, placeholder="Type your message..."),
41
  outputs=gr.Textbox(),
42
  title="BLACKLIGHT by v0id",
43
- description="Brutalist • Minimal • Precise — Clinical analysis by BLACKLIGHT"
44
  )
45
 
46
- # Enable queue for slow model loading
47
  iface.queue()
48
 
49
  if __name__ == "__main__":
50
- iface.launch()
 
1
  import gradio as gr
2
+ import torch
3
+ from transformers import AutoTokenizer, AutoModelForCausalLM, TextGenerationPipeline
4
 
5
+ # Pick a SMALL, CPU-friendly model to guarantee it runs on free hardware.
6
+ # You can swap to another small instruct model later if you like.
7
+ MODEL_ID = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
 
 
 
8
 
9
+ BLACKLIGHT_SYSTEM = (
 
10
  "System: You are BLACKLIGHT, created by v0id under AWAKEN CULT VISIONS. "
11
  "Always reply in the style of BLACKLIGHT: brutalist, minimal, precise.\n\n"
12
  "MODE: TRUTH\n"
 
14
  "Avoid metaphors or flowery language.\n\n"
15
  )
16
 
17
+ # ---- Load model on CPU (no accelerate, no device_map) ----
18
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
19
+ model = AutoModelForCausalLM.from_pretrained(
20
+ MODEL_ID,
21
+ torch_dtype=torch.float32, # CPU-safe dtype
22
+ low_cpu_mem_usage=False, # avoid accelerate path
23
+ trust_remote_code=True
24
+ )
25
+ pipe = TextGenerationPipeline(model=model, tokenizer=tokenizer, device=-1)
26
+
27
+ def chat(user_message: str):
28
+ user_message = (user_message or "").strip()
29
+ if not user_message:
30
  return "[Error: Empty prompt]"
31
+
32
+ prompt = f"{BLACKLIGHT_SYSTEM}User: {user_message}\nAssistant:"
33
  try:
34
+ out = pipe(
35
+ prompt,
36
+ max_new_tokens=192,
 
37
  temperature=0.7,
38
  top_p=0.9,
39
+ do_sample=True,
40
+ pad_token_id=tokenizer.eos_token_id,
41
  )
42
+ # TextGenerationPipeline returns a list of dicts: [{'generated_text': '...'}]
43
+ full_text = out[0]["generated_text"]
44
+ # Return ONLY the assistant portion so your frontend gets clean text
45
+ reply = full_text.split("Assistant:", 1)[-1].strip()
46
+ return reply or "[Error: Model returned empty text]"
47
  except Exception as e:
48
+ return f"[Error: {e}]"
49
 
50
+ # Gradio interface (simple single-text in/out ensures data[0] is a string)
51
  iface = gr.Interface(
52
+ fn=chat,
53
+ inputs=gr.Textbox(lines=2, placeholder="Type your message"),
54
  outputs=gr.Textbox(),
55
  title="BLACKLIGHT by v0id",
56
+ description="Brutalist • Minimal • Precise — Clinical analysis by BLACKLIGHT",
57
  )
58
 
59
+ # Enable queue; let HF serve the app (no custom FastAPI needed)
60
  iface.queue()
61
 
62
  if __name__ == "__main__":
63
+ iface.launch()