keenthinker commited on
Commit
c52e2e9
·
verified ·
1 Parent(s): 2a7edea

Update app.py

Browse files

Refactored summarizer to use a chat completion client

Files changed (1) hide show
  1. app.py +26 -14
app.py CHANGED
@@ -1,23 +1,16 @@
1
  from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
 
2
  import datetime
3
  import requests
4
  import pytz
5
  import yaml
6
  import re
 
7
  from tools.final_answer import FinalAnswerTool
8
  from tools.visit_webpage import VisitWebpageTool
9
 
10
  from Gradio_UI import GradioUI
11
 
12
- # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
13
- # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
14
- model = HfApiModel(
15
- max_tokens=2096,
16
- temperature=0.5,
17
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
18
- custom_role_conversions=None,
19
- )
20
-
21
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
22
  @tool
23
  def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
@@ -52,17 +45,36 @@ def summarize_text_tool(text:str)-> str:
52
  text: the text to be summaried
53
  """
54
  try:
55
- from transformers import pipeline
56
- summarizer = pipeline("summarization", model=model)
57
- summary = summarizer(text, max_length=150, min_length=50, do_sample=False)
58
- return summary[0]['summary_text']
 
 
 
 
 
 
 
 
 
 
 
59
  except Exception as e:
60
  return f"OH noes, something went wrong...:-/ {str(e)}"
61
 
62
-
63
  final_answer = FinalAnswerTool()
64
  visit_webpage = VisitWebpageTool()
65
 
 
 
 
 
 
 
 
 
 
66
  # Import tool from Hub
67
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
68
 
 
1
  from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
+ from huggingface_hub import InferenceClient
3
  import datetime
4
  import requests
5
  import pytz
6
  import yaml
7
  import re
8
+ import os
9
  from tools.final_answer import FinalAnswerTool
10
  from tools.visit_webpage import VisitWebpageTool
11
 
12
  from Gradio_UI import GradioUI
13
 
 
 
 
 
 
 
 
 
 
14
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
15
  @tool
16
  def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
 
45
  text: the text to be summaried
46
  """
47
  try:
48
+ hf_key = os.getEnv('hf_key')
49
+ client = InferenceClient(api_key=hf_key)
50
+ messages = [
51
+ {
52
+ "role": "user",
53
+ "content": f"Your are an experienced editor. Your are very skilled in summarizing texts and creating prompts from the content for LLM's to generate and image. Summarize the following text and create a prompt for an image generation: {text}"
54
+ }
55
+ ]
56
+ response = client.chat.completions.create(
57
+ model="Qwen/Qwen2.5-Coder-32B-Instruct",
58
+ messages=messages,
59
+ max_tokens=500
60
+ )
61
+ summarized_text = response.choices[0].message.content
62
+ return summarized_text
63
  except Exception as e:
64
  return f"OH noes, something went wrong...:-/ {str(e)}"
65
 
 
66
  final_answer = FinalAnswerTool()
67
  visit_webpage = VisitWebpageTool()
68
 
69
+ # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
70
+ # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
71
+ model = HfApiModel(
72
+ max_tokens=2096,
73
+ temperature=0.5,
74
+ model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
75
+ custom_role_conversions=None,
76
+ )
77
+
78
  # Import tool from Hub
79
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
80