han-ram commited on
Commit
c614eec
·
verified ·
1 Parent(s): 25a58e7

Upload Api_nlp_hybrid.py

Browse files
Files changed (1) hide show
  1. Api_nlp_hybrid.py +48 -37
Api_nlp_hybrid.py CHANGED
@@ -12,6 +12,7 @@ from Sastrawi.Stemmer.StemmerFactory import StemmerFactory
12
  from transformers import pipeline
13
  from youtube_comment_downloader import YoutubeCommentDownloader
14
  from fastapi import FastAPI
 
15
  from pydantic import BaseModel
16
 
17
  # LOAD CSV
@@ -104,12 +105,6 @@ def hybrid_predict(text):
104
  if lr_prob < 0.90:
105
  use_bert = True
106
 
107
- # # teks mengandung slang
108
- # slang_words = ["msh", "pake", "gk", "ga", "bgt"]
109
-
110
- # if any(word in clean.split() for word in slang_words):
111
- # use_bert = True
112
-
113
  # INDOBERT
114
 
115
  if use_bert:
@@ -167,50 +162,66 @@ def predict(data: TextRequest):
167
  @app.post("/analyze-youtube")
168
  def analyze_youtube(data: YoutubeRequest):
169
 
170
- downloader = YoutubeCommentDownloader()
171
 
172
- comments = downloader.get_comments_from_url(data.url)
173
 
174
- comments = list(comments)
175
 
176
- results = []
177
 
178
- positif = 0
179
- negatif = 0
180
- netral = 0
 
 
 
181
 
182
- for item in comments:
183
 
184
- text = item.get("text", "")
 
 
185
 
186
- if text.strip() == "":
187
- continue
188
 
189
- sentiment = hybrid_predict(text)
190
 
191
- if sentiment == "positif":
192
- positif += 1
193
 
194
- elif sentiment == "negatif":
195
- negatif += 1
196
 
197
- else:
198
- netral += 1
199
 
200
- results.append({
201
- "comment": text,
202
- "sentiment": sentiment
203
- })
204
 
205
- return {
 
 
 
 
 
 
206
 
207
- "total_comments": len(results),
 
 
 
 
 
 
 
 
 
 
 
208
 
209
- "summary": {
210
- "positif": positif,
211
- "negatif": negatif,
212
- "netral": netral
213
- },
214
 
215
- "results": results
216
- }
 
 
 
12
  from transformers import pipeline
13
  from youtube_comment_downloader import YoutubeCommentDownloader
14
  from fastapi import FastAPI
15
+ from fastapi import HTTPException
16
  from pydantic import BaseModel
17
 
18
  # LOAD CSV
 
105
  if lr_prob < 0.90:
106
  use_bert = True
107
 
 
 
 
 
 
 
108
  # INDOBERT
109
 
110
  if use_bert:
 
162
  @app.post("/analyze-youtube")
163
  def analyze_youtube(data: YoutubeRequest):
164
 
165
+ try:
166
 
167
+ downloader = YoutubeCommentDownloader()
168
 
169
+ comments_generator = downloader.get_comments_from_url(data.url)
170
 
171
+ comments = list(comments_generator)
172
 
173
+ if not comments:
174
+ return {
175
+ "message": "Komentar tidak ditemukan",
176
+ "total_comments": 0,
177
+ "results": []
178
+ }
179
 
180
+ results = []
181
 
182
+ positif = 0
183
+ negatif = 0
184
+ netral = 0
185
 
186
+ for item in comments:
 
187
 
188
+ text = item.get("text", "")
189
 
190
+ if text.strip() == "":
191
+ continue
192
 
193
+ sentiment = hybrid_predict(text)
 
194
 
195
+ if sentiment == "positif":
196
+ positif += 1
197
 
198
+ elif sentiment == "negatif":
199
+ negatif += 1
 
 
200
 
201
+ else:
202
+ netral += 1
203
+
204
+ results.append({
205
+ "comment": text,
206
+ "sentiment": sentiment
207
+ })
208
 
209
+ return {
210
+
211
+ "total_comments": len(results),
212
+
213
+ "summary": {
214
+ "positif": positif,
215
+ "negatif": negatif,
216
+ "netral": netral
217
+ },
218
+
219
+ "results": results
220
+ }
221
 
222
+ except Exception as e:
 
 
 
 
223
 
224
+ raise HTTPException(
225
+ status_code=500,
226
+ detail=str(e)
227
+ )