InnaV commited on
Commit
8cdb946
·
verified ·
1 Parent(s): c99f59a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -22
app.py CHANGED
@@ -1,33 +1,30 @@
1
- from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
- import datetime
3
- import requests
4
- import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
7
  from bertopic import BERTopic
8
-
9
  from Gradio_UI import GradioUI
10
 
 
 
 
11
  @tool
12
  def extract_main_topic(arg1: str) -> str:
13
- """Extracts the main topic from a given text using BERTopic.
 
14
 
15
  Args:
16
- text (str): The input text to analyze.
17
 
18
  Returns:
19
- str: The extracted main topic as keywords.
20
  """
21
-
22
- # Initialize BERTopic model
23
- topic_model = BERTopic.load("MaartenGr/BERTopic_ArXiv")
24
-
25
  topics, probs = topic_model.transform([arg1])
 
26
 
27
  if topic_id == -1: # BERTopic assigns -1 to noise (no topic detected)
28
  return "No clear topic detected."
29
 
30
- topic_words = topic_model.get_topic(topics[0])
31
 
32
  if topic_words:
33
  keywords = ", ".join([word for word, _ in topic_words])
@@ -38,21 +35,18 @@ def extract_main_topic(arg1: str) -> str:
38
  final_answer = FinalAnswerTool()
39
 
40
  model = HfApiModel(
41
- max_tokens=2096,
42
- temperature=0.5,
43
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
44
- custom_role_conversions=None,
45
  )
46
 
47
- # Import tool from Hub
48
- image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
49
-
50
  with open("prompts.yaml", 'r') as stream:
51
  prompt_templates = yaml.safe_load(stream)
52
 
53
  agent = CodeAgent(
54
  model=model,
55
- tools=[final_answer, extract_main_topic],
56
  max_steps=6,
57
  verbosity_level=1,
58
  grammar=None,
@@ -62,4 +56,4 @@ agent = CodeAgent(
62
  prompt_templates=prompt_templates
63
  )
64
 
65
- GradioUI(agent).launch()
 
1
+ from smolagents import CodeAgent, HfApiModel, load_tool, tool
 
 
 
2
  import yaml
3
  from tools.final_answer import FinalAnswerTool
4
  from bertopic import BERTopic
 
5
  from Gradio_UI import GradioUI
6
 
7
+ # Load BERTopic model
8
+ topic_model = BERTopic.load("MaartenGr/BERTopic_ArXiv")
9
+
10
  @tool
11
  def extract_main_topic(arg1: str) -> str:
12
+ """
13
+ Extracts the main topic from a given text using BERTopic.
14
 
15
  Args:
16
+ arg1 (str): The input text to analyze.
17
 
18
  Returns:
19
+ str: The extracted main topic.
20
  """
 
 
 
 
21
  topics, probs = topic_model.transform([arg1])
22
+ topic_id = topics[0]
23
 
24
  if topic_id == -1: # BERTopic assigns -1 to noise (no topic detected)
25
  return "No clear topic detected."
26
 
27
+ topic_words = topic_model.get_topic(topic_id)
28
 
29
  if topic_words:
30
  keywords = ", ".join([word for word, _ in topic_words])
 
35
  final_answer = FinalAnswerTool()
36
 
37
  model = HfApiModel(
38
+ max_tokens=2096,
39
+ temperature=0.5,
40
+ model_id='Qwen/Qwen2.5-Coder-32B-Instruct', # This model may be overloaded
41
+ custom_role_conversions=None,
42
  )
43
 
 
 
 
44
  with open("prompts.yaml", 'r') as stream:
45
  prompt_templates = yaml.safe_load(stream)
46
 
47
  agent = CodeAgent(
48
  model=model,
49
+ tools=[final_answer, my_custom_tool, extract_main_topic], # Added extract_main_topic
50
  max_steps=6,
51
  verbosity_level=1,
52
  grammar=None,
 
56
  prompt_templates=prompt_templates
57
  )
58
 
59
+ GradioUI(agent).launch()