-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
358 lines (313 loc) · 9.98 KB
/
utils.go
File metadata and controls
358 lines (313 loc) · 9.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"net/http"
"os"
"strings"
"sync"
"time"
"github.com/PuerkitoBio/goquery"
log "github.com/sirupsen/logrus"
)
var (
apiKey = flag.String("newsApiKey", os.Getenv("NEWS_API_KEY"), "Provide News Api Key")
query = flag.String("query", "Syrian War", "Entery query to search")
hgfApiKey = flag.String("hgfApiKey", os.Getenv("HUGGINGFACE_API_KEY"), "Provide Hugging Face Api Key")
onlyScrape = flag.Bool("onlyScrape", false, "Only scrape articles without calling any apis")
outputFile = flag.String("o", "./scrape.json", "Enter a path to save scraped articles")
)
type Article struct {
Source struct {
ID string `json:"id"`
Name string `json:"name"`
} `json:"source"`
Author string `json:"author"`
Title string `json:"title"`
Description string `json:"description"`
URL string `json:"url"`
URLToImage string `json:"urlToImage"`
PublishedAt string `json:"publishedAt"`
Content string `json:"content"`
}
type NewAPIResponse struct {
Status string `json:"status"`
TotalResults int `json:"totalResults"`
Articles []Article `json:"articles"`
}
type ProcessedArticle struct {
URL string `json:"url"`
Title string `json:"title"`
Content string `json:"content"`
Summary string `json:"summary"`
Label string `json:"label"`
Score float64 `json:"score"`
Methods map[string]float64 `json:"methods"`
}
func FetchArticles(apiKey, query string) ([]Article, error) {
url := fmt.Sprintf("https://newsapi.org/v2/everything?q=%s&apiKey=%s", query, apiKey)
res, err := http.Get(url)
if err != nil {
return nil, fmt.Errorf("Error fetching articles: %v", err)
}
defer res.Body.Close()
var newResponse NewAPIResponse
if err := json.NewDecoder(res.Body).Decode(&newResponse); err != nil {
return nil, fmt.Errorf("Error decoding response: %v", err)
}
return newResponse.Articles, nil
}
func SaveArticles(filename string, articles []Article) error {
data, err := json.MarshalIndent(articles, "", " ")
if err != nil {
return fmt.Errorf("Error marshaling articles: %v", err)
}
return os.WriteFile(filename, data, 0o644)
}
func LoadArticles(filename string) ([]Article, error) {
data, err := os.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("Error reading file %s: %v", filename, err)
}
var articles []Article
if err := json.Unmarshal(data, &articles); err != nil {
return nil, fmt.Errorf("Error unmarshaling articles: %v", err)
}
return articles, nil
}
func ScrapeArticle(url string) (string, error) {
res, err := http.Get(url)
if err != nil {
return "", fmt.Errorf("Error fetching URL %s: %v", url, err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return "", fmt.Errorf("non-200 response code %d | status code: %v", &err, res.StatusCode)
}
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
return "", fmt.Errorf("error parsing HTML: %v", err)
}
var contentBuilder strings.Builder
doc.Find("p").Each(func(i int, s *goquery.Selection) {
text := s.Text()
contentBuilder.WriteString(text + "\n")
})
return contentBuilder.String(), nil
}
func saveScrapedContent(filename string, articles []ProcessedArticle) error {
var scrapedData []map[string]string
for _, article := range articles {
data := map[string]string{
"url": article.URL,
"content": article.Content,
}
scrapedData = append(scrapedData, data)
}
data, err := json.MarshalIndent(scrapedData, "", " ")
if err != nil {
return fmt.Errorf("Error marshaling scraped content %v", err)
}
return os.WriteFile(filename, data, 0o644)
}
func splitTextIntoChunks(text string, maxTokens int) []string {
words := strings.Fields(text)
var chunks []string
for i := 0; i < len(words); i += maxTokens {
end := i + maxTokens
if end > len(words) {
end = len(words)
}
chunk := strings.Join(words[i:end], " ")
chunks = append(chunks, chunk)
}
return chunks
}
func callHuggingFaceAPI(endpoint string, payload []byte) ([]byte, error) {
req, err := http.NewRequest("POST", endpoint, bytes.NewBuffer(payload))
if err != nil {
return nil, fmt.Errorf("error creating request: %v", err)
}
req.Header.Set("Authorization", "Bearer "+*hgfApiKey)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{Timeout: 60 * time.Second}
res, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("error making request to Hugging Face API: %v", err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
bodyBytes, _ := io.ReadAll(res.Body)
return nil, fmt.Errorf("non-200 response from Hugging Face API: %d - %s", res.StatusCode, string(bodyBytes))
}
return io.ReadAll(res.Body)
}
func callSummarizationAPI(text string) (string, error) {
endpoint := "https://api-inference.huggingface.co/models/facebook/bart-large-cnn"
payload := map[string]interface{}{
"inputs": text,
"parameters": map[string]interface{}{
"max_length": 150,
"min_length": 50,
"do_sample": false,
},
}
payloadBytes, _ := json.Marshal(payload)
responseBytes, err := callHuggingFaceAPI(endpoint, payloadBytes)
if err != nil {
return "", fmt.Errorf("Error while getting response from HuggingFaceAPI endpoint: %v", err)
}
var result []map[string]string
if err := json.Unmarshal(responseBytes, &result); err != nil {
return "", fmt.Errorf("error unmarshiling summary: %v", err)
}
if len(result) > 0 {
return result[0]["summary_text"], nil
}
return "", fmt.Errorf("empty summary")
}
func SummarizeText(text string) (string, error) {
chunks := splitTextIntoChunks(text, 500)
var summaryParts []string
for _, chunk := range chunks {
summary, err := callSummarizationAPI(chunk)
if err != nil {
return "", err
}
summaryParts = append(summaryParts, summary)
}
combinedSummary := strings.Join(summaryParts, " ")
finalSummary, err := callSummarizationAPI(combinedSummary)
if err != nil {
return "", err
}
return finalSummary, nil
}
func classifyText(text string, labels []string, multiLabel bool) (map[string]float64, error) {
endpoint := "https://api-inference.huggingface.co/models/facebook/bart-large-mnli"
payload := map[string]interface{}{
"inputs": text,
"parameters": map[string]interface{}{
"candidate_labels": labels,
"multi_label": multiLabel,
},
}
payloadBytes, _ := json.Marshal(payload)
responseBytes, err := callHuggingFaceAPI(endpoint, payloadBytes)
if err != nil {
return nil, err
}
var result struct {
Labels []string `json:"labels"`
Scores []float64 `json:"scores"`
}
if err := json.Unmarshal(responseBytes, &result); err != nil {
return nil, fmt.Errorf("error unmarshaling classification response: %v", err)
}
classification := make(map[string]float64)
for i, label := range result.Labels {
classification[label] = result.Scores[i]
}
return classification, nil
}
func processArticles(articles []Article, onlyScrape bool) ([]ProcessedArticle, error) {
var wg sync.WaitGroup
resultsChan := make(chan ProcessedArticle, len(articles))
limit := 10
sem := make(chan struct{}, limit)
for _, article := range articles {
article := article
wg.Add(1)
go func() {
defer wg.Done()
sem <- struct{}{}
defer func() { <-sem }()
log.Printf("Processing: %s - %s\n", article.Title, article.URL)
content, err := ScrapeArticle(article.URL)
if err != nil {
log.Errorf("Error scraping article: %v\n", err)
return
}
if onlyScrape {
processedArticle := ProcessedArticle{
URL: article.URL,
Content: content,
}
resultsChan <- processedArticle
return
}
summary, err := SummarizeText(content)
if err != nil {
log.Errorf("Error summarizing article: %v\n", err)
return
}
techniques := []string{
"name-calling", "bandwagon", "fear appeal", "appeal to authority",
"glittering generalities", "plain folks appeal", "testimonial", "loaded language",
}
methods, err := classifyText(content, techniques, true)
if err != nil {
log.Errorf("Error classifying propaganda methods: %v\n", err)
return
}
labels := []string{"propaganda", "neutral"}
labelScores, err := classifyText(content, labels, false)
if err != nil {
log.Errorf("Error classifying article: %v\n", err)
return
}
label := labels[0]
score := labelScores[label]
if labelScores[labels[1]] > score {
label = labels[1]
score = labelScores[label]
}
processedArticle := ProcessedArticle{
URL: article.URL,
Title: article.Title,
Content: content,
Summary: summary,
Label: label,
Score: score,
Methods: methods,
}
resultsChan <- processedArticle
}()
}
go func() {
wg.Wait()
close(resultsChan)
}()
var processedArticles []ProcessedArticle
for processedArticle := range resultsChan {
processedArticles = append(processedArticles, processedArticle)
}
return processedArticles, nil
}
func saveProcessedArticles(filename string, articles []ProcessedArticle) error {
data, err := json.MarshalIndent(articles, "", " ")
if err != nil {
return fmt.Errorf("error marshaling processed articles: %v", err)
}
return os.WriteFile(filename, data, 0o644)
}
func generateSummaryReport(filename string, articles []ProcessedArticle) error {
var reportBuilder strings.Builder
reportBuilder.WriteString("# Propaganda Analysis Report\n\n")
for _, article := range articles {
reportBuilder.WriteString(fmt.Sprintf("## %s\n", article.Title))
reportBuilder.WriteString(fmt.Sprintf("**Label**: %s\n", article.Label))
reportBuilder.WriteString(fmt.Sprintf("**URL**: %s\n\n", article.URL))
reportBuilder.WriteString(fmt.Sprintf("**Summary**: %s\n\n", article.Summary))
reportBuilder.WriteString(fmt.Sprintf("**Propaganda Score**: %.2f\n", article.Score))
reportBuilder.WriteString("**Propaganda Methods**:\n")
for method, score := range article.Methods {
reportBuilder.WriteString(fmt.Sprintf("- %s: %.2f\n", method, score))
}
reportBuilder.WriteString("\n---\n\n")
}
return os.WriteFile(filename, []byte(reportBuilder.String()), 0o644)
}