noranisa commited on
Commit
acfb9de
·
verified ·
1 Parent(s): 1f3b000

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +44 -6
main.py CHANGED
@@ -1,7 +1,11 @@
1
- from flask import Flask, render_template, request
2
  from services.aggregator import collect_data
3
  from services.sentiment import predict
4
  from collections import Counter
 
 
 
 
5
 
6
  app = Flask(__name__)
7
 
@@ -11,17 +15,50 @@ def index():
11
  keyword = request.form.get('keyword')
12
  source = request.form.get('source', 'all')
13
 
14
- comments = collect_data(keyword, source)
15
 
16
- sentiments = predict(comments[:50])
 
 
 
17
 
18
  counts = Counter(sentiments)
19
- data = list(zip(comments[:50], sentiments))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  return render_template(
22
  "result.html",
23
  data=data,
24
  counts=counts,
 
 
25
  source=source,
26
  keyword=keyword
27
  )
@@ -29,5 +66,6 @@ def index():
29
  return render_template("index.html")
30
 
31
 
32
- if __name__ == "__main__":
33
- app.run(host="0.0.0.0", port=7860)
 
 
1
+ from flask import Flask, render_template, request, send_file
2
  from services.aggregator import collect_data
3
  from services.sentiment import predict
4
  from collections import Counter
5
+ import pandas as pd
6
+ from wordcloud import WordCloud
7
+ import matplotlib.pyplot as plt
8
+ from sklearn.metrics import classification_report
9
 
10
  app = Flask(__name__)
11
 
 
15
  keyword = request.form.get('keyword')
16
  source = request.form.get('source', 'all')
17
 
18
+ data_raw = collect_data(keyword, source)
19
 
20
+ texts = [t for s, t in data_raw][:100]
21
+ sources = [s for s, t in data_raw][:100]
22
+
23
+ sentiments = predict(texts)
24
 
25
  counts = Counter(sentiments)
26
+
27
+ # 📊 PER PLATFORM
28
+ platform_counts = {}
29
+ for src, sent in zip(sources, sentiments):
30
+ if src not in platform_counts:
31
+ platform_counts[src] = {"Positive":0,"Neutral":0,"Negative":0}
32
+ platform_counts[src][sent] += 1
33
+
34
+ # ☁️ WORDCLOUD
35
+ try:
36
+ text_all = " ".join(texts)
37
+ wc = WordCloud(width=800, height=400).generate(text_all)
38
+ wc.to_file("static/wordcloud.png")
39
+ except:
40
+ pass
41
+
42
+ # 📁 SAVE CSV
43
+ df = pd.DataFrame({
44
+ "text": texts,
45
+ "sentiment": sentiments,
46
+ "source": sources
47
+ })
48
+ df.to_csv("static/result.csv", index=False)
49
+
50
+ # 📊 EVALUASI (dummy ground truth)
51
+ y_true = sentiments # karena tidak ada label asli
52
+ report = classification_report(y_true, sentiments, output_dict=True)
53
+
54
+ data = list(zip(texts, sentiments, sources))
55
 
56
  return render_template(
57
  "result.html",
58
  data=data,
59
  counts=counts,
60
+ platform_counts=platform_counts,
61
+ report=report,
62
  source=source,
63
  keyword=keyword
64
  )
 
66
  return render_template("index.html")
67
 
68
 
69
+ @app.route('/download')
70
+ def download():
71
+ return send_file("static/result.csv", as_attachment=True)