Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,54 +1,66 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import gradio as ui
|
| 3 |
-
from
|
| 4 |
|
| 5 |
-
# Target model ID definition
|
| 6 |
MODEL_ID = "coder-vansh/cypher_llm_model"
|
| 7 |
-
|
| 8 |
-
# Fetch secure token from space settings automatically
|
| 9 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
def predict(message, history):
|
| 15 |
-
# Perfect prompt engineering template layout for CYPHER format parsing
|
| 16 |
system_prompt = "You are CYPHER, a casual, witty Hinglish AI companion by Vansh & Aditya. Tagline: \"Not just an AI β YOUR AI.\""
|
| 17 |
|
| 18 |
-
# Building
|
| 19 |
messages = [{"role": "system", "content": system_prompt}]
|
| 20 |
-
|
| 21 |
-
# Restoring past history blocks if any exist
|
| 22 |
if history:
|
| 23 |
for user_msg, bot_msg in history:
|
| 24 |
messages.append({"role": "user", "content": user_msg})
|
| 25 |
messages.append({"role": "assistant", "content": bot_msg})
|
| 26 |
|
| 27 |
-
# Appending the latest query
|
| 28 |
messages.append({"role": "user", "content": message})
|
| 29 |
|
| 30 |
try:
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
stream=False
|
| 37 |
)
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
except Exception as e:
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
elif "401" in error_msg or "403" in error_msg:
|
| 47 |
-
return "π Token Issue: Bhai lagta hai tumne Space Settings me 'HF_TOKEN' naam ka Secret sahi se add nahi kiya hai!"
|
| 48 |
-
else:
|
| 49 |
-
return f"π‘ Network glitch trace: {error_msg}\n\n(Bhai agar Model loading status hai toh 1 min wait karke retry karo!)"
|
| 50 |
-
|
| 51 |
-
# Elegant web layout container
|
| 52 |
with ui.Blocks() as demo:
|
| 53 |
ui.Markdown("# π€ CYPHER AI Live Production Space")
|
| 54 |
ui.Markdown("### *Not just an AI β YOUR AI.* | Developed by Vansh & Aditya")
|
|
@@ -56,8 +68,7 @@ with ui.Blocks() as demo:
|
|
| 56 |
ui.ChatInterface(
|
| 57 |
fn=predict,
|
| 58 |
textbox=ui.Textbox(placeholder="Bhai se kuch bhi poocho...", container=False, scale=7),
|
| 59 |
-
description="CYPHER is running
|
| 60 |
)
|
| 61 |
|
| 62 |
-
# Launching web application frame
|
| 63 |
demo.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
+
import torch
|
| 3 |
import gradio as ui
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
| 5 |
|
|
|
|
| 6 |
MODEL_ID = "coder-vansh/cypher_llm_model"
|
|
|
|
|
|
|
| 7 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 8 |
|
| 9 |
+
print("βοΈ System: Initializing 4-bit Quantization engine to save Cloud RAM...")
|
| 10 |
+
bnb_config = BitsAndBytesConfig(
|
| 11 |
+
load_in_4bit=True,
|
| 12 |
+
bnb_4bit_quant_type="nf4",
|
| 13 |
+
bnb_4bit_compute_dtype=torch.float16,
|
| 14 |
+
bnb_4bit_use_double_quant=True
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
print("π₯ Loading Model weights natively into memory space...")
|
| 18 |
+
# Load tokenizer and model weights inside the local basic CPU instance
|
| 19 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, token=HF_TOKEN)
|
| 20 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 21 |
+
MODEL_ID,
|
| 22 |
+
quantization_config=bnb_config,
|
| 23 |
+
low_cpu_mem_usage=True,
|
| 24 |
+
token=HF_TOKEN
|
| 25 |
+
)
|
| 26 |
+
print("β
CYPHER Brain is now natively alive inside the Space environment!")
|
| 27 |
|
| 28 |
def predict(message, history):
|
|
|
|
| 29 |
system_prompt = "You are CYPHER, a casual, witty Hinglish AI companion by Vansh & Aditya. Tagline: \"Not just an AI β YOUR AI.\""
|
| 30 |
|
| 31 |
+
# Building exact chat block matching schema
|
| 32 |
messages = [{"role": "system", "content": system_prompt}]
|
|
|
|
|
|
|
| 33 |
if history:
|
| 34 |
for user_msg, bot_msg in history:
|
| 35 |
messages.append({"role": "user", "content": user_msg})
|
| 36 |
messages.append({"role": "assistant", "content": bot_msg})
|
| 37 |
|
|
|
|
| 38 |
messages.append({"role": "user", "content": message})
|
| 39 |
|
| 40 |
try:
|
| 41 |
+
inputs = tokenizer.apply_chat_template(
|
| 42 |
+
messages,
|
| 43 |
+
tokenize=True,
|
| 44 |
+
add_generation_prompt=True,
|
| 45 |
+
return_tensors="pt"
|
|
|
|
| 46 |
)
|
| 47 |
|
| 48 |
+
with torch.no_grad():
|
| 49 |
+
outputs = model.generate(
|
| 50 |
+
input_ids=inputs,
|
| 51 |
+
max_new_tokens=250,
|
| 52 |
+
temperature=0.7,
|
| 53 |
+
do_sample=True,
|
| 54 |
+
pad_token_id=tokenizer.eos_token_id
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
response = tokenizer.decode(outputs[0][inputs.shape[1]:], skip_special_tokens=True)
|
| 58 |
+
return response
|
| 59 |
|
| 60 |
except Exception as e:
|
| 61 |
+
return f"β οΈ Execution Glitch: {str(e)}"
|
| 62 |
+
|
| 63 |
+
# Elegant interface container
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
with ui.Blocks() as demo:
|
| 65 |
ui.Markdown("# π€ CYPHER AI Live Production Space")
|
| 66 |
ui.Markdown("### *Not just an AI β YOUR AI.* | Developed by Vansh & Aditya")
|
|
|
|
| 68 |
ui.ChatInterface(
|
| 69 |
fn=predict,
|
| 70 |
textbox=ui.Textbox(placeholder="Bhai se kuch bhi poocho...", container=False, scale=7),
|
| 71 |
+
description="CYPHER is running 100% Natively inside your cloud container.",
|
| 72 |
)
|
| 73 |
|
|
|
|
| 74 |
demo.launch()
|