Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,7 +26,7 @@ class BasicAgent:
|
|
| 26 |
def __call__(self, question: str) -> str:
|
| 27 |
"""透過 HF Inference API 呼叫模型"""
|
| 28 |
if self.agent is None:
|
| 29 |
-
return "HF_TOKEN not configured
|
| 30 |
|
| 31 |
try:
|
| 32 |
headers = {"Authorization": f"Bearer {self.hf_token}"}
|
|
@@ -54,16 +54,12 @@ class BasicAgent:
|
|
| 54 |
|
| 55 |
if isinstance(result, list) and len(result) > 0:
|
| 56 |
answer = result[0].get("generated_text", "")
|
| 57 |
-
# 去除問題部分,只保留答案
|
| 58 |
answer = answer.replace(question, "").strip()
|
| 59 |
-
return answer[:1000] if answer else "No answer
|
| 60 |
|
| 61 |
return "Invalid response"
|
| 62 |
|
| 63 |
-
except requests.exceptions.Timeout:
|
| 64 |
-
return "API timeout - try again"
|
| 65 |
except Exception as e:
|
| 66 |
-
print(f"Error: {e}")
|
| 67 |
return f"Error: {str(e)[:100]}"
|
| 68 |
|
| 69 |
|
|
@@ -74,6 +70,8 @@ def run_and_submit_all(profile: Optional[gr.OAuthProfile] = None):
|
|
| 74 |
return "Please Login to Hugging Face with the button.", None
|
| 75 |
|
| 76 |
username = profile.username
|
|
|
|
|
|
|
| 77 |
space_id = os.getenv("SPACE_ID", "s1144662")
|
| 78 |
|
| 79 |
api_url = DEFAULT_API_URL
|
|
@@ -102,7 +100,7 @@ def run_and_submit_all(profile: Optional[gr.OAuthProfile] = None):
|
|
| 102 |
task_id = item.get("task_id")
|
| 103 |
question_text = item.get("question")
|
| 104 |
|
| 105 |
-
print(f"[{idx}/{total}] Processing
|
| 106 |
|
| 107 |
try:
|
| 108 |
submitted_answer = agent(question_text)
|
|
@@ -116,15 +114,14 @@ def run_and_submit_all(profile: Optional[gr.OAuthProfile] = None):
|
|
| 116 |
"Answer": submitted_answer[:200]
|
| 117 |
})
|
| 118 |
except Exception as e:
|
| 119 |
-
error_answer = f"Error: {str(e)}"
|
| 120 |
answers_payload.append({
|
| 121 |
"task_id": task_id,
|
| 122 |
-
"submitted_answer":
|
| 123 |
})
|
| 124 |
results_log.append({
|
| 125 |
"Task ID": task_id,
|
| 126 |
"Question": question_text[:80],
|
| 127 |
-
"Answer":
|
| 128 |
})
|
| 129 |
|
| 130 |
submission_data = {
|
|
|
|
| 26 |
def __call__(self, question: str) -> str:
|
| 27 |
"""透過 HF Inference API 呼叫模型"""
|
| 28 |
if self.agent is None:
|
| 29 |
+
return "HF_TOKEN not configured!"
|
| 30 |
|
| 31 |
try:
|
| 32 |
headers = {"Authorization": f"Bearer {self.hf_token}"}
|
|
|
|
| 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]}"
|
| 64 |
|
| 65 |
|
|
|
|
| 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
|
|
|
|
| 100 |
task_id = item.get("task_id")
|
| 101 |
question_text = item.get("question")
|
| 102 |
|
| 103 |
+
print(f"[{idx}/{total}] Processing...")
|
| 104 |
|
| 105 |
try:
|
| 106 |
submitted_answer = agent(question_text)
|
|
|
|
| 114 |
"Answer": submitted_answer[:200]
|
| 115 |
})
|
| 116 |
except Exception as e:
|
|
|
|
| 117 |
answers_payload.append({
|
| 118 |
"task_id": task_id,
|
| 119 |
+
"submitted_answer": str(e)
|
| 120 |
})
|
| 121 |
results_log.append({
|
| 122 |
"Task ID": task_id,
|
| 123 |
"Question": question_text[:80],
|
| 124 |
+
"Answer": str(e)[:100]
|
| 125 |
})
|
| 126 |
|
| 127 |
submission_data = {
|