Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,14 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
-
from transformers import
|
| 4 |
|
| 5 |
# -------------------------
|
| 6 |
# CONFIGURATION
|
| 7 |
# -------------------------
|
| 8 |
-
MODEL_ID = "Qwen/
|
| 9 |
-
|
| 10 |
-
# -------------------------
|
| 11 |
-
# TOKEN AUTHENTICATION
|
| 12 |
-
# -------------------------
|
| 13 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
|
|
|
| 14 |
if not HF_TOKEN:
|
| 15 |
raise ValueError("🚫 Hugging Face token not found. Please add 'HF_TOKEN' in your Space secrets.")
|
| 16 |
|
|
@@ -18,17 +16,14 @@ if not HF_TOKEN:
|
|
| 18 |
# LOAD TOKENIZER & MODEL
|
| 19 |
# -------------------------
|
| 20 |
try:
|
| 21 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 22 |
-
MODEL_ID,
|
| 23 |
-
trust_remote_code=True,
|
| 24 |
-
token=HF_TOKEN
|
| 25 |
-
)
|
| 26 |
-
|
| 27 |
model = AutoModelForCausalLM.from_pretrained(
|
| 28 |
MODEL_ID,
|
|
|
|
| 29 |
trust_remote_code=True,
|
| 30 |
-
device_map="
|
| 31 |
-
|
|
|
|
| 32 |
)
|
| 33 |
except Exception as e:
|
| 34 |
raise RuntimeError(f"🚨 Failed to load model: {e}")
|
|
@@ -40,19 +35,24 @@ pipe = pipeline(
|
|
| 40 |
"text-generation",
|
| 41 |
model=model,
|
| 42 |
tokenizer=tokenizer,
|
| 43 |
-
max_new_tokens=
|
| 44 |
do_sample=True,
|
| 45 |
-
temperature=0.7
|
| 46 |
)
|
| 47 |
|
| 48 |
# -------------------------
|
| 49 |
# MAIN ASSISTANT FUNCTION
|
| 50 |
# -------------------------
|
| 51 |
def ai_assistant(command: str) -> str:
|
| 52 |
-
prompt = f"
|
|
|
|
| 53 |
try:
|
| 54 |
output = pipe(prompt)[0]["generated_text"]
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
return response
|
| 57 |
except Exception as e:
|
| 58 |
return f"⚠️ Error: {e}"
|
|
@@ -64,9 +64,9 @@ demo = gr.Interface(
|
|
| 64 |
fn=ai_assistant,
|
| 65 |
inputs=gr.Textbox(lines=2, placeholder="e.g. Open Chrome or Take a screenshot"),
|
| 66 |
outputs="text",
|
| 67 |
-
title="🧠 Qwen
|
| 68 |
-
description="
|
| 69 |
-
|
| 70 |
)
|
| 71 |
|
| 72 |
# -------------------------
|
|
|
|
| 1 |
import os
|
| 2 |
+
import torch
|
| 3 |
import gradio as gr
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 5 |
|
| 6 |
# -------------------------
|
| 7 |
# CONFIGURATION
|
| 8 |
# -------------------------
|
| 9 |
+
MODEL_ID = "Qwen/Qwen1.5-1.8B-Chat" # small + fast for CPU
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 11 |
+
|
| 12 |
if not HF_TOKEN:
|
| 13 |
raise ValueError("🚫 Hugging Face token not found. Please add 'HF_TOKEN' in your Space secrets.")
|
| 14 |
|
|
|
|
| 16 |
# LOAD TOKENIZER & MODEL
|
| 17 |
# -------------------------
|
| 18 |
try:
|
| 19 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, token=HF_TOKEN, trust_remote_code=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
model = AutoModelForCausalLM.from_pretrained(
|
| 21 |
MODEL_ID,
|
| 22 |
+
token=HF_TOKEN,
|
| 23 |
trust_remote_code=True,
|
| 24 |
+
device_map="cpu",
|
| 25 |
+
torch_dtype=torch.float32,
|
| 26 |
+
low_cpu_mem_usage=True
|
| 27 |
)
|
| 28 |
except Exception as e:
|
| 29 |
raise RuntimeError(f"🚨 Failed to load model: {e}")
|
|
|
|
| 35 |
"text-generation",
|
| 36 |
model=model,
|
| 37 |
tokenizer=tokenizer,
|
| 38 |
+
max_new_tokens=100,
|
| 39 |
do_sample=True,
|
| 40 |
+
temperature=0.7
|
| 41 |
)
|
| 42 |
|
| 43 |
# -------------------------
|
| 44 |
# MAIN ASSISTANT FUNCTION
|
| 45 |
# -------------------------
|
| 46 |
def ai_assistant(command: str) -> str:
|
| 47 |
+
prompt = f"<|user|>\n{command}\n<|assistant|>\n"
|
| 48 |
+
|
| 49 |
try:
|
| 50 |
output = pipe(prompt)[0]["generated_text"]
|
| 51 |
+
# Get assistant's part only
|
| 52 |
+
if "<|assistant|>" in output:
|
| 53 |
+
response = output.split("<|assistant|>")[-1].strip()
|
| 54 |
+
else:
|
| 55 |
+
response = output.strip()
|
| 56 |
return response
|
| 57 |
except Exception as e:
|
| 58 |
return f"⚠️ Error: {e}"
|
|
|
|
| 64 |
fn=ai_assistant,
|
| 65 |
inputs=gr.Textbox(lines=2, placeholder="e.g. Open Chrome or Take a screenshot"),
|
| 66 |
outputs="text",
|
| 67 |
+
title="🧠 Qwen 1.5 AI Assistant",
|
| 68 |
+
description="Ask anything. This assistant uses the Qwen1.5-1.8B-Chat model on CPU.",
|
| 69 |
+
flagging_mode="never"
|
| 70 |
)
|
| 71 |
|
| 72 |
# -------------------------
|