MacKov commited on
Commit
b4e9757
·
verified ·
1 Parent(s): a196080

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +114 -11
app.py CHANGED
@@ -1,12 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  class BasicAgent:
2
  def __init__(self):
3
  print("Инициализация агента...")
4
 
5
  self.model = InferenceClientModel(
6
- model_id="mistralai/Mixtral-8x7B-Instruct-v0.1", # ← новая модель
7
  token=os.getenv("HF_TOKEN"),
8
  temperature=0.05,
9
- max_tokens=768,
10
  )
11
 
12
  tools = [
@@ -15,7 +35,6 @@ class BasicAgent:
15
  PythonInterpreterTool(),
16
  ]
17
 
18
- # Скачивание файлов (без изменений, но работает)
19
  @tool
20
  def download_file(url: str) -> str:
21
  """
@@ -84,20 +103,23 @@ class BasicAgent:
84
  tools=tools,
85
  model=self.model,
86
  add_base_tools=True,
87
- max_steps=15,
88
  )
89
  print("Агент готов!")
90
 
91
  def __call__(self, question: str) -> str:
92
  print(f"Вопрос: {question[:120]}...")
93
 
 
94
  if len(question) > 2500:
95
  question = question[:2500] + "\n[Обрезано из-за длины.]"
96
 
 
97
  q = question.lower()
98
  if any(k in q for k in [".mp3", "audio", "recording", "voice", "youtube.com", "video", "attached", "file", "excel", "pdf", "image", "jpg", "png"]):
99
- question += "\nЕсли есть URL или attached файл — обязательно используй download_file, затем read_pdf или read_excel. Отвечай только по фактам из файла. Не придумывай."
100
 
 
101
  if "chess" in q or "image" in q or ".jpg" in q or ".png" in q:
102
  question += "\nЕсли есть URL изображения — скачай и опиши позицию или ищи похожую. Не придумывай ход."
103
 
@@ -105,6 +127,7 @@ class BasicAgent:
105
  result = self.agent.run(question)
106
  answer = str(result).strip()
107
 
 
108
  prefixes = [
109
  "Final Answer", "Final answer", "Answer:", "The answer is",
110
  "So the final answer is", "```", "boxed{", "}", "[/INST]", "</s>",
@@ -119,16 +142,96 @@ class BasicAgent:
119
  answer = answer[1:-1].strip()
120
 
121
  answer = answer.strip()
122
- if len(answer) > 300 or "придум" in answer.lower():
 
123
  answer = answer[:150] + "..." if len(answer) > 150 else answer
124
 
125
  print(f"Ответ: {answer[:150]}...")
126
  return answer or "Нет ответа"
127
 
128
  except Exception as e:
129
- err_str = str(e)
130
- if "402" in err_str or "Payment Required" in err_str:
131
- return "Лимит API исчерпан. Смени модель на Mixtral-8x7B или подожди."
132
- err = f"Ошибка: {err_str[:200]}"
133
  print(err)
134
- return err
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ import requests
4
+ import pandas as pd
5
+ from pathlib import Path
6
+
7
+ from smolagents import (
8
+ CodeAgent,
9
+ InferenceClientModel,
10
+ DuckDuckGoSearchTool,
11
+ VisitWebpageTool,
12
+ PythonInterpreterTool,
13
+ tool
14
+ )
15
+
16
+ from pypdf import PdfReader
17
+
18
+ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
19
+ MODEL_ID = "Qwen/Qwen2.5-32B-Instruct"
20
+
21
  class BasicAgent:
22
  def __init__(self):
23
  print("Инициализация агента...")
24
 
25
  self.model = InferenceClientModel(
26
+ model_id=MODEL_ID,
27
  token=os.getenv("HF_TOKEN"),
28
  temperature=0.05,
29
+ max_tokens=1024,
30
  )
31
 
32
  tools = [
 
35
  PythonInterpreterTool(),
36
  ]
37
 
 
38
  @tool
39
  def download_file(url: str) -> str:
40
  """
 
103
  tools=tools,
104
  model=self.model,
105
  add_base_tools=True,
106
+ max_steps=18,
107
  )
108
  print("Агент готов!")
109
 
110
  def __call__(self, question: str) -> str:
111
  print(f"Вопрос: {question[:120]}...")
112
 
113
+ # Обрезка длинных вопросов
114
  if len(question) > 2500:
115
  question = question[:2500] + "\n[Обрезано из-за длины.]"
116
 
117
+ # Хак для файлов/видео/аудио/attached
118
  q = question.lower()
119
  if any(k in q for k in [".mp3", "audio", "recording", "voice", "youtube.com", "video", "attached", "file", "excel", "pdf", "image", "jpg", "png"]):
120
+ question += "\nЕсли есть URL или attached файл — скачай, прочитай и отвечай только по фактам из него. Не придумывай числа, имена или данные."
121
 
122
+ # Хак для шахмат
123
  if "chess" in q or "image" in q or ".jpg" in q or ".png" in q:
124
  question += "\nЕсли есть URL изображения — скачай и опиши позицию или ищи похожую. Не придумывай ход."
125
 
 
127
  result = self.agent.run(question)
128
  answer = str(result).strip()
129
 
130
+ # Жёсткая очистка + защита от длинных ответов
131
  prefixes = [
132
  "Final Answer", "Final answer", "Answer:", "The answer is",
133
  "So the final answer is", "```", "boxed{", "}", "[/INST]", "</s>",
 
142
  answer = answer[1:-1].strip()
143
 
144
  answer = answer.strip()
145
+ # Если ответ слишком длинный или подозрительный обрезаем
146
+ if len(answer) > 300 or "придум" in answer.lower() or answer.count(",") > 20:
147
  answer = answer[:150] + "..." if len(answer) > 150 else answer
148
 
149
  print(f"Ответ: {answer[:150]}...")
150
  return answer or "Нет ответа"
151
 
152
  except Exception as e:
153
+ err = f"Ошибка: {str(e)[:200]}"
 
 
 
154
  print(err)
155
+ return err
156
+
157
+ # --- run_and_submit_all (без изменений) ---
158
+ def run_and_submit_all(profile: gr.OAuthProfile | None):
159
+ space_id = os.getenv("SPACE_ID")
160
+ if profile:
161
+ username = profile.username
162
+ print(f"Вход: {username}")
163
+ else:
164
+ return "Войдите в HF", None
165
+
166
+ api_url = DEFAULT_API_URL
167
+ questions_url = f"{api_url}/questions"
168
+ submit_url = f"{api_url}/submit"
169
+
170
+ try:
171
+ agent = BasicAgent()
172
+ except Exception as e:
173
+ return f"Ошибка агента: {e}", None
174
+
175
+ agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
176
+
177
+ try:
178
+ resp = requests.get(questions_url, timeout=15)
179
+ resp.raise_for_status()
180
+ questions = resp.json()
181
+ if not questions:
182
+ return "Вопросов нет", None
183
+ print(f"Вопросов: {len(questions)}")
184
+ except Exception as e:
185
+ return f"О��ибка вопросов: {e}", None
186
+
187
+ results = []
188
+ payload = []
189
+ for item in questions:
190
+ tid = item.get("task_id")
191
+ q = item.get("question")
192
+ if not tid or not q:
193
+ continue
194
+ try:
195
+ ans = agent(q)
196
+ payload.append({"task_id": tid, "submitted_answer": ans})
197
+ results.append({"Task ID": tid, "Question": q, "Answer": ans})
198
+ except Exception as e:
199
+ results.append({"Task ID": tid, "Question": q, "Answer": f"ERROR: {e}"})
200
+
201
+ if not payload:
202
+ return "Нет ответов", pd.DataFrame(results)
203
+
204
+ data = {"username": username.strip(), "agent_code": agent_code, "answers": payload}
205
+
206
+ try:
207
+ resp = requests.post(submit_url, json=data, timeout=60)
208
+ resp.raise_for_status()
209
+ res = resp.json()
210
+ status = (
211
+ f"Успех!\n"
212
+ f"Пользователь: {res.get('username')}\n"
213
+ f"Балл: {res.get('score', 'N/A')}% "
214
+ f"({res.get('correct_count', '?')}/{res.get('total_attempted', '?')})\n"
215
+ f"{res.get('message', '')}"
216
+ )
217
+ return status, pd.DataFrame(results)
218
+ except Exception as e:
219
+ return f"Ошибка отправки: {e}", pd.DataFrame(results)
220
+
221
+ # --- Gradio ---
222
+ with gr.Blocks() as demo:
223
+ gr.Markdown("# Агент для финального задания")
224
+ gr.Markdown("""
225
+ 1. Клонируй и дорабатывай.
226
+ 2. Войди через кнопку.
227
+ 3. Нажми кнопку — увидишь score.
228
+ """)
229
+ gr.LoginButton()
230
+ btn = gr.Button("Запустить оценку и отправить")
231
+ status = gr.Textbox(label="Результат", lines=6)
232
+ table = gr.DataFrame(label="Ответы", wrap=True)
233
+ btn.click(run_and_submit_all, outputs=[status, table])
234
+
235
+ if __name__ == "__main__":
236
+ print("Запуск...")
237
+ demo.launch(debug=True, share=False)