AiWicked commited on
Commit
aa4cf9c
·
verified ·
1 Parent(s): 572dfbb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -17
app.py CHANGED
@@ -10,29 +10,53 @@ from Gradio_UI import GradioUI
10
  # Below is an example of a tool that does nothing. Amaze us with your creativity!
11
 
12
  @tool
13
- def generate_keywords(topic: str, max_keywords: int) -> str:
14
- """A tool that generates the most relevant keywords based on a given topic.
15
  Args:
16
- topic: the subject or theme for which keywords need to be generated
17
- max_keywords: the maximum number of keywords to generate
18
  """
19
- # Basic preprocessing to remove common stopwords (without external libraries)
20
- stopwords = set(["и", "в", "на", "с", "по", "для", "как", "что", "это", "а", "но", "я", "у", "за", "из", "о", "об", "от"])
 
 
 
 
 
 
 
 
21
 
22
- # Splitting the topic into words and filtering out short words and stopwords
23
- words = topic.lower().split()
24
- filtered_words = [word for word in words if len(word) > 2 and word not in stopwords]
25
 
26
- # Generating unique keywords by frequency and limiting to max_keywords
27
- keyword_frequency = {}
28
- for word in filtered_words:
29
- keyword_frequency[word] = keyword_frequency.get(word, 0) + 1
30
 
31
- # Sorting keywords by frequency (descending) and selecting top ones
32
- sorted_keywords = sorted(keyword_frequency.keys(), key=lambda x: keyword_frequency[x], reverse=True)
33
- top_keywords = sorted_keywords[:max_keywords]
34
 
35
- return ", ".join(top_keywords) if top_keywords else "Нет релевантных ключевых слов"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
 
38
  @tool
 
10
  # Below is an example of a tool that does nothing. Amaze us with your creativity!
11
 
12
  @tool
13
+ def generate_keywords(topic: str, num_keywords: int = 5) -> str:
14
+ """Generates relevant keywords based on the given topic
15
  Args:
16
+ topic: The main topic or subject to generate keywords for
17
+ num_keywords: Number of keywords to generate (default: 5)
18
  """
19
+ # Basic word transformations and common patterns
20
+ prefixes = ['best', 'top', 'how to', 'why', 'what is']
21
+ suffixes = ['guide', 'tutorial', 'examples', 'tips', 'basics']
22
+ related_terms = {
23
+ 'programming': ['coding', 'development', 'software', 'algorithms', 'debugging'],
24
+ 'business': ['marketing', 'strategy', 'management', 'startup', 'planning'],
25
+ 'technology': ['tech', 'innovation', 'digital', 'software', 'hardware'],
26
+ 'education': ['learning', 'teaching', 'study', 'course', 'training'],
27
+ 'health': ['wellness', 'fitness', 'nutrition', 'medical', 'healthcare']
28
+ }
29
 
30
+ # Clean and normalize the input topic
31
+ topic = topic.lower().strip()
 
32
 
33
+ keywords = set()
 
 
 
34
 
35
+ # Add the original topic
36
+ keywords.add(topic)
 
37
 
38
+ # Add prefix + topic combinations
39
+ for prefix in prefixes:
40
+ keywords.add(f"{prefix} {topic}")
41
+
42
+ # Add topic + suffix combinations
43
+ for suffix in suffixes:
44
+ keywords.add(f"{topic} {suffix}")
45
+
46
+ # Add related terms if the topic matches any category
47
+ for category, terms in related_terms.items():
48
+ if topic in terms or category in topic:
49
+ keywords.update(terms)
50
+
51
+ # Select the most relevant keywords
52
+ final_keywords = list(keywords)[:num_keywords]
53
+
54
+ # Format the output
55
+ return "\n".join([
56
+ "Generated keywords:",
57
+ "----------------",
58
+ *[f"- {keyword}" for keyword in final_keywords]
59
+ ])
60
 
61
 
62
  @tool