Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,11 +1,11 @@
|
|
| 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
|
| 8 |
-
from sklearn.metrics import classification_report
|
| 9 |
|
| 10 |
app = Flask(__name__)
|
| 11 |
|
|
@@ -24,22 +24,22 @@ def index():
|
|
| 24 |
|
| 25 |
counts = Counter(sentiments)
|
| 26 |
|
| 27 |
-
# π
|
| 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 |
-
# βοΈ
|
| 35 |
try:
|
| 36 |
-
|
| 37 |
-
wc = WordCloud(width=800, height=400).generate(
|
| 38 |
wc.to_file("static/wordcloud.png")
|
| 39 |
except:
|
| 40 |
pass
|
| 41 |
|
| 42 |
-
# π
|
| 43 |
df = pd.DataFrame({
|
| 44 |
"text": texts,
|
| 45 |
"sentiment": sentiments,
|
|
@@ -47,9 +47,8 @@ def index():
|
|
| 47 |
})
|
| 48 |
df.to_csv("static/result.csv", index=False)
|
| 49 |
|
| 50 |
-
# π
|
| 51 |
-
|
| 52 |
-
report = classification_report(y_true, sentiments, output_dict=True)
|
| 53 |
|
| 54 |
data = list(zip(texts, sentiments, sources))
|
| 55 |
|
|
@@ -58,9 +57,9 @@ def index():
|
|
| 58 |
data=data,
|
| 59 |
counts=counts,
|
| 60 |
platform_counts=platform_counts,
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
)
|
| 65 |
|
| 66 |
return render_template("index.html")
|
|
@@ -68,4 +67,8 @@ def index():
|
|
| 68 |
|
| 69 |
@app.route('/download')
|
| 70 |
def download():
|
| 71 |
-
return send_file("static/result.csv", as_attachment=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 services.evaluation import evaluate_model
|
| 5 |
from collections import Counter
|
| 6 |
import pandas as pd
|
| 7 |
from wordcloud import WordCloud
|
| 8 |
+
import os
|
|
|
|
| 9 |
|
| 10 |
app = Flask(__name__)
|
| 11 |
|
|
|
|
| 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 |
+
os.makedirs("static", exist_ok=True)
|
| 37 |
+
wc = WordCloud(width=800, height=400).generate(" ".join(texts))
|
| 38 |
wc.to_file("static/wordcloud.png")
|
| 39 |
except:
|
| 40 |
pass
|
| 41 |
|
| 42 |
+
# π CSV
|
| 43 |
df = pd.DataFrame({
|
| 44 |
"text": texts,
|
| 45 |
"sentiment": sentiments,
|
|
|
|
| 47 |
})
|
| 48 |
df.to_csv("static/result.csv", index=False)
|
| 49 |
|
| 50 |
+
# π evaluasi
|
| 51 |
+
eval_result = evaluate_model(predict)
|
|
|
|
| 52 |
|
| 53 |
data = list(zip(texts, sentiments, sources))
|
| 54 |
|
|
|
|
| 57 |
data=data,
|
| 58 |
counts=counts,
|
| 59 |
platform_counts=platform_counts,
|
| 60 |
+
eval_result=eval_result,
|
| 61 |
+
keyword=keyword,
|
| 62 |
+
source=source
|
| 63 |
)
|
| 64 |
|
| 65 |
return render_template("index.html")
|
|
|
|
| 67 |
|
| 68 |
@app.route('/download')
|
| 69 |
def download():
|
| 70 |
+
return send_file("static/result.csv", as_attachment=True)
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
if __name__ == "__main__":
|
| 74 |
+
app.run(host="0.0.0.0", port=7860)
|