s1144662 commited on
Commit
7e35574
·
verified ·
1 Parent(s): a6d9840

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -9
app.py CHANGED
@@ -12,7 +12,8 @@ class BasicAgent:
12
  def __init__(self):
13
  """用 HuggingFace Inference API"""
14
  self.hf_token = os.getenv("HF_TOKEN")
15
- self.model_name = "meta-llama/Llama-2-7b-chat-hf"
 
16
 
17
  if not self.hf_token:
18
  print("✗ HF_TOKEN not found")
@@ -21,7 +22,7 @@ class BasicAgent:
21
 
22
  self.agent_url = f"{HF_API_URL}/{self.model_name}"
23
  self.agent = True
24
- print("✓ Agent initialized with HF API")
25
 
26
  def __call__(self, question: str) -> str:
27
  """透過 HF Inference API 呼叫模型"""
@@ -35,8 +36,6 @@ class BasicAgent:
35
  "inputs": question,
36
  "parameters": {
37
  "max_new_tokens": 256,
38
- "temperature": 0.7,
39
- "top_p": 0.9
40
  }
41
  }
42
 
@@ -47,17 +46,20 @@ class BasicAgent:
47
  timeout=60
48
  )
49
 
 
 
 
50
  if response.status_code != 200:
51
- return f"API Error {response.status_code}"
52
 
53
  result = response.json()
54
 
55
  if isinstance(result, list) and len(result) > 0:
56
  answer = result[0].get("generated_text", "")
57
  answer = answer.replace(question, "").strip()
58
- return answer[:1000] if answer else "No answer"
59
 
60
- return "Invalid response"
61
 
62
  except Exception as e:
63
  return f"Error: {str(e)[:100]}"
@@ -70,8 +72,6 @@ def run_and_submit_all(profile: Optional[gr.OAuthProfile] = None):
70
  return "Please Login to Hugging Face with the button.", None
71
 
72
  username = profile.username
73
-
74
- # 自動抓 SPACE_ID(不用手動設定)
75
  space_id = os.getenv("SPACE_ID", "s1144662")
76
 
77
  api_url = DEFAULT_API_URL
 
12
  def __init__(self):
13
  """用 HuggingFace Inference API"""
14
  self.hf_token = os.getenv("HF_TOKEN")
15
+ # 改用更穩定的小模型
16
+ self.model_name = "gpt2" # 或試試 "google/flan-t5-base"
17
 
18
  if not self.hf_token:
19
  print("✗ HF_TOKEN not found")
 
22
 
23
  self.agent_url = f"{HF_API_URL}/{self.model_name}"
24
  self.agent = True
25
+ print(f"✓ Agent initialized with model: {self.model_name}")
26
 
27
  def __call__(self, question: str) -> str:
28
  """透過 HF Inference API 呼叫模型"""
 
36
  "inputs": question,
37
  "parameters": {
38
  "max_new_tokens": 256,
 
 
39
  }
40
  }
41
 
 
46
  timeout=60
47
  )
48
 
49
+ if response.status_code == 410:
50
+ return "Model loading... please try again"
51
+
52
  if response.status_code != 200:
53
+ return f"API Error {response.status_code}: {response.text[:100]}"
54
 
55
  result = response.json()
56
 
57
  if isinstance(result, list) and len(result) > 0:
58
  answer = result[0].get("generated_text", "")
59
  answer = answer.replace(question, "").strip()
60
+ return answer[:1000] if answer else "No answer generated"
61
 
62
+ return "Invalid response format"
63
 
64
  except Exception as e:
65
  return f"Error: {str(e)[:100]}"
 
72
  return "Please Login to Hugging Face with the button.", None
73
 
74
  username = profile.username
 
 
75
  space_id = os.getenv("SPACE_ID", "s1144662")
76
 
77
  api_url = DEFAULT_API_URL