timsmykov commited on
Commit
1e5420b
·
verified ·
1 Parent(s): adc610d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -65
app.py CHANGED
@@ -1,80 +1,40 @@
1
- from smolagents import CodeAgent, HfApiModel, load_tool
2
  import yaml
3
  from smolagents import tool
4
  from duckduckgo_search import DDGS
 
5
 
6
  from tools.final_answer import FinalAnswerTool
7
  from Gradio_UI import GradioUI
8
 
9
- @tool
10
- def DuckDuckGoSearchTool(query: str) -> str:
11
- """
12
- Инструмент для поиска информации в интернете с помощью DuckDuckGo.
13
- Args:
14
- query: Поисковый запрос.
15
- """
16
- with DDGS() as ddgs:
17
- results = [r for r in ddgs.text(query, max_results=5)] # Ограничиваем до 5 результатов
18
- if not results:
19
- return "По вашему запросу ничего не найдено."
20
- formatted_results = "\n\n".join(
21
- f"**Заголовок:** {r['title']}\n**Ссылка:** {r['href']}\n**Краткое содержание:** {r['body']}"
22
- for r in results
23
- )
24
- return formatted_results
25
 
26
- final_answer = FinalAnswerTool()
 
 
 
27
 
28
- # Define both model IDs
29
- primary_model_id = 'Qwen/Qwen2.5-Coder-32B-Instruct'
30
- fallback_model_id = 'https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
 
 
 
 
 
 
 
 
 
31
 
32
- # Try to initialize the primary model
33
- try:
34
- model = HfApiModel(
35
- max_tokens=2096,
36
- temperature=0.5,
37
- model_id=primary_model_id,
38
- custom_role_conversions=None,
39
- )
40
- print(f"Using primary model: {primary_model_id}")
41
- except Exception as e:
42
- print(f"Error initializing primary model: {e}")
43
- print(f"Falling back to secondary model: {fallback_model_id}")
44
- # If the primary model fails, use the fallback model
45
- model = HfApiModel(
46
- max_tokens=2096,
47
- temperature=0.5,
48
- model_id=fallback_model_id,
49
- custom_role_conversions=None,
50
- )
51
 
52
- image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
53
-
54
- # Загрузка prompt_templates из prompts.yaml
55
- with open("prompts.yaml", 'r') as stream:
56
- try:
57
- prompt_templates = yaml.safe_load(stream)
58
- except yaml.YAMLError as exc:
59
- print(exc)
60
-
61
- # Проверка, что prompt_templates имеет правильный формат
62
- if isinstance(prompt_templates, dict) and 'system_prompt' in prompt_templates:
63
- # Если все ок, используем загруженный шаблон
64
- system_prompt = prompt_templates['system_prompt']
65
-
66
- prompt_templates = {'system_prompt': system_prompt}
67
 
68
  agent = CodeAgent(
69
- model=model,
70
- tools=[DuckDuckGoSearchTool, final_answer, image_generation_tool],
71
- max_steps=6,
72
- verbosity_level=1,
73
- grammar=None,
74
- planning_interval=None,
75
- name=None,
76
- description=None,
77
- prompt_templates=prompt_templates
78
  )
79
 
80
- GradioUI(agent).launch()
 
1
+ from smolagents import CodeAgent, load_tool
2
  import yaml
3
  from smolagents import tool
4
  from duckduckgo_search import DDGS
5
+ import requests
6
 
7
  from tools.final_answer import FinalAnswerTool
8
  from Gradio_UI import GradioUI
9
 
10
+ #... (your existing code)...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
+ class DeepSeekModel:
13
+ def __init__(self, api_key):
14
+ self.api_key = api_key
15
+ self.url = "https://api.deepseek.ai/v1/chat/completions" # Replace with the correct API endpoint
16
 
17
+ def generate(self, messages):
18
+ headers = {
19
+ "Authorization": f"Bearer {self.api_key}",
20
+ "Content-Type": "application/json"
21
+ }
22
+ data = {
23
+ "messages": messages,
24
+ # Add any other required parameters for the DeepSeek API
25
+ }
26
+ response = requests.post(self.url, headers=headers, json=data)
27
+ response.raise_for_status() # Raise an exception for bad status codes
28
+ return response.json() # Assuming the response is in JSON format
29
 
30
+ # Initialize the DeepSeek model
31
+ model = DeepSeekModel(api_key="sk-eea44c5fc6be4e289cf0c1ae5bd91b58")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
+ #... (rest of your code)...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
  agent = CodeAgent(
36
+ model=model, # Use the DeepSeekModel instance
37
+ #... (rest of your agent parameters)...
 
 
 
 
 
 
 
38
  )
39
 
40
+ #... (rest of your code)...