Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,32 +14,40 @@ from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
|
| 14 |
|
| 15 |
class BasicAgent:
|
| 16 |
def __init__(self):
|
| 17 |
-
print("
|
| 18 |
|
| 19 |
-
model_id = "
|
| 20 |
|
| 21 |
-
# Load model and tokenizer
|
| 22 |
self.tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 23 |
self.model = AutoModelForCausalLM.from_pretrained(model_id)
|
| 24 |
|
| 25 |
-
# Create
|
| 26 |
self.pipeline = pipeline(
|
| 27 |
"text-generation",
|
| 28 |
model=self.model,
|
| 29 |
tokenizer=self.tokenizer,
|
| 30 |
-
device=-1
|
| 31 |
)
|
| 32 |
|
| 33 |
def __call__(self, question: str) -> str:
|
| 34 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 35 |
|
| 36 |
try:
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
generated_text = output[0]["generated_text"]
|
| 42 |
-
response = generated_text.split("
|
| 43 |
return response
|
| 44 |
|
| 45 |
except Exception as e:
|
|
|
|
| 14 |
|
| 15 |
class BasicAgent:
|
| 16 |
def __init__(self):
|
| 17 |
+
print("Zephyr Local Agent initialized.")
|
| 18 |
|
| 19 |
+
model_id = "HuggingFaceH4/zephyr-7b-beta"
|
| 20 |
|
| 21 |
+
# Load model and tokenizer
|
| 22 |
self.tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 23 |
self.model = AutoModelForCausalLM.from_pretrained(model_id)
|
| 24 |
|
| 25 |
+
# Create generation pipeline
|
| 26 |
self.pipeline = pipeline(
|
| 27 |
"text-generation",
|
| 28 |
model=self.model,
|
| 29 |
tokenizer=self.tokenizer,
|
| 30 |
+
device=-1 # CPU
|
| 31 |
)
|
| 32 |
|
| 33 |
def __call__(self, question: str) -> str:
|
| 34 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 35 |
|
| 36 |
try:
|
| 37 |
+
# Format for chat-style prompt
|
| 38 |
+
prompt = f"<|system|>You are a helpful assistant.<|user|>{question.strip()}<|assistant|>"
|
| 39 |
+
|
| 40 |
+
output = self.pipeline(
|
| 41 |
+
prompt,
|
| 42 |
+
max_new_tokens=256,
|
| 43 |
+
do_sample=True,
|
| 44 |
+
temperature=0.7,
|
| 45 |
+
top_p=0.95
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
# Return model's response only (strip off prompt)
|
| 49 |
generated_text = output[0]["generated_text"]
|
| 50 |
+
response = generated_text.split("<|assistant|>")[-1].strip()
|
| 51 |
return response
|
| 52 |
|
| 53 |
except Exception as e:
|