alex-abb commited on
Commit
73f9174
·
verified ·
1 Parent(s): 2e937f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -56
app.py CHANGED
@@ -1,16 +1,12 @@
1
  import requests
2
- from bs4 import BeautifulSoup
3
- import gradio as gr
4
- import os
5
-
6
 
 
7
  api_token = os.environ.get("TOKEN")
8
  API_URL = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-8B-Instruct"
9
  headers = {"Authorization": f"Bearer {api_token}"}
10
 
11
- url = "https://huggingface.co/posts"
12
-
13
-
14
  def query(payload):
15
  response = requests.post(API_URL, headers=headers, json=payload)
16
  return response.json()
@@ -19,66 +15,115 @@ def analyze_sentiment(text):
19
  output = query({
20
  "inputs": f'''
21
  system
22
- you are going to analyse the prompt that I'll give to you and tell me if they are either talking about "chat bot", "AI dev", or something else.
 
 
 
 
 
 
 
 
 
 
23
 
24
  user
25
  {text}
26
 
27
  assistant
 
28
  '''
29
  })
30
 
31
  if isinstance(output, list) and len(output) > 0:
32
  response = output[0].get('generated_text', '').strip().lower()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
- if "chat bot" in response:
35
- return "chat bot"
36
- elif "ai dev" in response:
37
- return "AI dev"
38
- else:
39
- return "autre"
40
- return "autre"
41
-
42
- def scrape_and_analyze(url):
43
- try:
44
- response = requests.get(url)
45
- if response.status_code != 200:
46
- return f"Erreur lors de la requête : {response.status_code}"
47
-
48
- soup = BeautifulSoup(response.text, 'html.parser')
49
-
50
- # Ajustez ce sélecteur selon la structure du site cible
51
- posts = soup.find_all('div', class_='cursor-pointer')
52
-
53
- categories = {"chat bot": 0, "AI dev": 0, "autre": 0}
54
- total_posts = 0
55
- result = ""
56
-
57
- for post in posts:
58
- total_posts += 1
59
- content = post.find('div', class_='relative').text.strip() if post.find('div', class_='relative') else "Pas de contenu"
60
-
61
- # Analyse du texte
62
- category = analyze_sentiment(content)
63
- categories[category] += 1
64
-
65
- # Affichage en temps réel
66
- print(f"Post {total_posts} analysé. Catégorie : {category}")
67
- print(f"Compteurs actuels : {categories}")
68
- print("---")
69
-
70
- # Ajout des résultats à la chaîne finale
71
- result += f"Post {total_posts} : Catégorie {category}\n"
72
-
73
- # Résultat final
74
- result += f"\nTotal des posts analysés : {total_posts}\n"
75
- for cat, count in categories.items():
76
- result += f"{cat} : {count}\n"
77
 
78
- return result
79
- except Exception as e:
80
- return f"Une erreur s'est produite : {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
 
82
- result = scrape_and_analyze(url)
83
- print(result)
 
84
 
 
 
 
 
 
1
  import requests
2
+ import json
3
+ import os
 
 
4
 
5
+ # Votre analyseur de post
6
  api_token = os.environ.get("TOKEN")
7
  API_URL = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-8B-Instruct"
8
  headers = {"Authorization": f"Bearer {api_token}"}
9
 
 
 
 
10
  def query(payload):
11
  response = requests.post(API_URL, headers=headers, json=payload)
12
  return response.json()
 
15
  output = query({
16
  "inputs": f'''
17
  system
18
+ You're going to deeply analyze the texts I'm going to give you and you're only going to tell me which category they belong to by answering only the words that correspond to the following categories:
19
+ For posts that talk about chat models/LLM, return "Chatmodel/LLM"
20
+ For posts that talk about image generation models, return "image_generation"
21
+ For texts that ask for information from the community, return "questions"
22
+ For posts about fine-tuning or model adjustment, return "fine_tuning"
23
+ For posts related to ethics and bias in AI, return "ethics_bias"
24
+ For posts about datasets and data preparation, return "datasets"
25
+ For posts about tools and libraries, return "tools_libraries"
26
+ For posts containing tutorials and guides, return "tutorials_guides"
27
+ For posts about debugging and problem-solving, return "debugging"
28
+ Respond only with the category name, without any additional explanation or text.
29
 
30
  user
31
  {text}
32
 
33
  assistant
34
+
35
  '''
36
  })
37
 
38
  if isinstance(output, list) and len(output) > 0:
39
  response = output[0].get('generated_text', '').strip().lower()
40
+ return response
41
+
42
+ return "Erreur: Réponse vide ou non valide"
43
+
44
+ def fetch_posts(limit=10, max_posts=50):
45
+ url = "https://huggingface.co/api/posts"
46
+ params = {'limit': limit}
47
+ offset = 0
48
+ all_posts = []
49
+
50
+ while len(all_posts) < max_posts:
51
+ params['offset'] = offset
52
+ response = requests.get(url, params=params)
53
+ data = response.json()
54
 
55
+ if not data['socialPosts']:
56
+ break
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
+ all_posts.extend(data['socialPosts'])
59
+ offset += len(data['socialPosts'])
60
+
61
+ return all_posts[:max_posts]
62
+
63
+ def display_post(post):
64
+ print(f"\n{'=' * 50}")
65
+ print(f"Contenu:\n{post['rawContent']}")
66
+ print(f"Likes: {sum(reaction['count'] for reaction in post['reactions'])}")
67
+ print(f"URL: {post['url']}")
68
+ print(f"Analyse: {post.get('analysis', 'Analyse non disponible')}")
69
+ print(f"Catégorie: {post.get('category', 'Non analysé')}")
70
+ print(f"{'=' * 50}\n")
71
+
72
+ # Récupérer les posts
73
+ posts = fetch_posts(limit=10, max_posts=30) # Récupère jusqu'à 30 posts
74
+
75
+ # Stocker les posts dans des variables différentes
76
+ post_variables = {f'post_{i}': post for i, post in enumerate(posts)}
77
+
78
+ # Dictionnaire pour compter les catégories
79
+ category_count = {
80
+ 'questions': 0,
81
+ 'Chatmodel/LLM': 0,
82
+ 'image_generation': 0,
83
+ 'fine_tuning': 0,
84
+ 'ethics_bias': 0,
85
+ 'datasets': 0,
86
+ 'tools_libraries': 0,
87
+ 'tutorials_guides': 0,
88
+ 'debugging': 0,
89
+ 'Erreur: Réponse ambiguë': 0,
90
+ 'Erreur: Réponse vide ou non valide': 0
91
+ }
92
+
93
+ # Analyser chaque post et ajouter les résultats de l'analyse au dictionnaire
94
+ for key in post_variables:
95
+ category = analyze_sentiment(post_variables[key]['rawContent'])
96
+
97
+ if category == 'questions':
98
+ category_count['questions'] += 2
99
+ elif category == 'Chatmodel/LLM':
100
+ category_count['Chatmodel/LLM'] += 2
101
+ elif category == 'image_generation':
102
+ category_count['image_generation'] += 2
103
+ elif category == 'fine_tuning':
104
+ category_count['fine_tuning'] += 2
105
+ elif category == 'ethics_bias':
106
+ category_count['ethics_bias'] += 2
107
+ elif category == 'datasets':
108
+ category_count['datasets'] += 2
109
+ elif category == 'tools_libraries':
110
+ category_count['tools_libraries'] += 2
111
+ elif category == 'tutorials_guides':
112
+ category_count['tutorials_guides'] += 2
113
+ elif category == 'debugging':
114
+ category_count['debugging'] += 2
115
+ elif category == "Erreur: Réponse ambiguë":
116
+ category_count["Erreur: Réponse ambiguë"] += 2
117
+ elif category == "Erreur: Réponse vide ou non valide":
118
+ category_count["Erreur: Réponse vide ou non valide"] += 2
119
+
120
+ post_variables[key]['category'] = category
121
 
122
+ # Imprimer un post spécifique
123
+ post_to_print = 'post_2' # Par exemple, le troisième post (index 2)
124
+ display_post(post_variables[post_to_print])
125
 
126
+ # Afficher le nombre de posts par catégorie
127
+ print("\nNombre de posts par catégorie:")
128
+ for category, count in category_count.items():
129
+ print(f"{category}: {count}")