inank commited on
Commit
937b418
·
verified ·
1 Parent(s): f9614ee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -10
app.py CHANGED
@@ -4,19 +4,65 @@ import requests
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
 
7
 
8
  from Gradio_UI import GradioUI
9
 
10
- # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
  @tool
12
- def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
13
- #Keep this format for the description / args / args description but feel free to modify the tool
14
- """A tool that does nothing yet
 
 
 
15
  Args:
16
- arg1: the first argument
17
- arg2: the second argument
 
 
 
18
  """
19
- return "What magic will you build ?"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  @tool
22
  def get_current_time_in_timezone(timezone: str) -> str:
@@ -52,10 +98,10 @@ image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_co
52
 
53
  with open("prompts.yaml", 'r') as stream:
54
  prompt_templates = yaml.safe_load(stream)
55
-
56
  agent = CodeAgent(
57
  model=model,
58
- tools=[final_answer], ## add your tools here (don't remove final answer)
59
  max_steps=6,
60
  verbosity_level=1,
61
  grammar=None,
@@ -66,4 +112,4 @@ agent = CodeAgent(
66
  )
67
 
68
 
69
- GradioUI(agent).launch()
 
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
7
+ from tools.pdf_extractor import extract_text_from_pdf
8
 
9
  from Gradio_UI import GradioUI
10
 
 
11
  @tool
12
+ def summarize_and_analyze_text(text: str, max_sentences: int = 5) -> str:
13
+ """Analyzes and summarizes text content, extracting key information and main ideas.
14
+
15
+ This tool intelligently condenses lengthy text into concise summaries while preserving
16
+ the most important information. Perfect for processing search results, PDFs, and documents.
17
+
18
  Args:
19
+ text: The text content to summarize and analyze
20
+ max_sentences: Maximum number of sentences in the summary (default: 5)
21
+
22
+ Returns:
23
+ A formatted summary containing key points and main ideas from the text
24
  """
25
+ try:
26
+ # Remove extra whitespace and normalize text
27
+ text = " ".join(text.split())
28
+
29
+ if len(text) < 100:
30
+ return f"Text is too short to summarize. Original text:\n{text}"
31
+
32
+ # Split into sentences (simple approach)
33
+ sentences = []
34
+ import re
35
+ for sent in re.split(r'(?<=[.!?])\s+', text):
36
+ sent = sent.strip()
37
+ if sent:
38
+ sentences.append(sent)
39
+
40
+ # Score sentences based on word frequency
41
+ words = text.lower().split()
42
+ word_freq = {}
43
+ for word in words:
44
+ if len(word) > 3: # Filter short words
45
+ word_freq[word] = word_freq.get(word, 0) + 1
46
+
47
+ # Select top sentences
48
+ sentence_scores = []
49
+ for i, sent in enumerate(sentences):
50
+ score = sum(word_freq.get(word.lower(), 0) for word in sent.split())
51
+ sentence_scores.append((i, score, sent))
52
+
53
+ # Sort by original order but select based on scores
54
+ top_indices = sorted([idx for idx, _, _ in sorted(sentence_scores, key=lambda x: -x[1])[:max_sentences]])
55
+ summary_sentences = [sent for idx, _, sent in sentence_scores if idx in top_indices]
56
+
57
+ summary = " ".join(summary_sentences)
58
+
59
+ # Extract key entities (words that appear frequently)
60
+ sorted_words = sorted(word_freq.items(), key=lambda x: -x[1])
61
+ key_terms = ", ".join([word for word, _ in sorted_words[:5]])
62
+
63
+ return f"""📋 SUMMARY:\n{summary}\n\n🔑 KEY TERMS: {key_terms}\n\n📊 ANALYSIS:\n- Text length: {len(text)} characters\n- Total sentences: {len(sentences)}\n- Summary length: {len(summary_sentences)} sentences"""
64
+ except Exception as e:
65
+ return f"Error analyzing text: {str(e)}"
66
 
67
  @tool
68
  def get_current_time_in_timezone(timezone: str) -> str:
 
98
 
99
  with open("prompts.yaml", 'r') as stream:
100
  prompt_templates = yaml.safe_load(stream)
101
+
102
  agent = CodeAgent(
103
  model=model,
104
+ tools=[image_generation_tool,get_current_time_in_timezone,extract_text_from_pdf,summarize_and_analyze_text,final_answer], ## add your tools here (don't remove final answer)
105
  max_steps=6,
106
  verbosity_level=1,
107
  grammar=None,
 
112
  )
113
 
114
 
115
+ GradioUI(agent, "/tmp").launch()