Davit6174 commited on
Commit
b277f85
·
verified ·
1 Parent(s): 57d055c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -19
app.py CHANGED
@@ -23,27 +23,31 @@ class BasicAgent:
23
 
24
  class ZephyrAgent:
25
  def __init__(self):
26
- model_id = "HuggingFaceH4/zephyr-7b-beta"
27
- print(f"Loading model: {model_id}")
28
- self.tokenizer = AutoTokenizer.from_pretrained(model_id)
29
- self.pipeline = pipeline(
30
- "text-generation",
31
- model=model_id,
32
- tokenizer=self.tokenizer,
33
- torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32,
34
- device=0 if torch.cuda.is_available() else -1,
35
- max_new_tokens=512,
36
- temperature=0.7,
37
- top_p=0.9,
38
- )
39
- print("✅ ZephyrAgent initialized.")
40
 
41
  def __call__(self, question: str) -> str:
42
- prompt = f"<|system|>\nYou are a helpful AI assistant.\n<|user|>\n{question}\n<|assistant|>\n"
43
- print(f"🧠 Prompting ZephyrAgent:\n{prompt[:100]}...")
44
- response = self.pipeline(prompt, return_full_text=False)
45
- generated_text = response[0]['generated_text'].strip()
46
- return generated_text
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
  def run_and_submit_all( profile: gr.OAuthProfile | None):
49
  """
 
23
 
24
  class ZephyrAgent:
25
  def __init__(self):
26
+ self.api_url = "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta"
27
+ self.headers = {
28
+ "Authorization": f"Bearer {os.getenv('HF_TOKEN')}"
29
+ }
30
+ print("ZephyrAPI initialized using Inference API.")
 
 
 
 
 
 
 
 
 
31
 
32
  def __call__(self, question: str) -> str:
33
+ prompt = f"<|system|>\nYou are a helpful assistant.\n<|user|>\n{question}\n<|assistant|>\n"
34
+ payload = {
35
+ "inputs": prompt,
36
+ "parameters": {
37
+ "max_new_tokens": 256,
38
+ "temperature": 0.7,
39
+ "top_p": 0.9,
40
+ }
41
+ }
42
+
43
+ try:
44
+ response = requests.post(self.api_url, headers=self.headers, json=payload, timeout=60)
45
+ response.raise_for_status()
46
+ result = response.json()
47
+ return result[0]["generated_text"].split("<|assistant|>")[-1].strip()
48
+ except Exception as e:
49
+ print(f"Error: {e}")
50
+ return "⚠️ Model could not respond. Check API access or token."
51
 
52
  def run_and_submit_all( profile: gr.OAuthProfile | None):
53
  """