gajula21 commited on
Commit
118bc34
·
verified ·
1 Parent(s): fb303f3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -87
app.py CHANGED
@@ -1,95 +1,31 @@
1
- from fastapi import FastAPI, HTTPException
 
2
  from pydantic import BaseModel
3
- from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
4
- from dotenv import load_dotenv
5
- import os
6
  import torch
 
7
 
8
- load_dotenv()
9
- hf_token = os.getenv("HF_API_TOKEN")
10
 
11
- model_id = "gajula21/youtube-sentiment-model-telugu"
 
 
 
12
 
13
- sentiment_model = None
14
- label_mapping = None
15
 
16
- try:
17
- if hf_token is None:
18
- raise ValueError("HF_API_TOKEN not found in environment variables or .env file.")
19
-
20
- device = "cpu"
21
- print(f"Loading model to device: {device}")
22
-
23
- tokenizer = AutoTokenizer.from_pretrained(model_id, token=hf_token, trust_remote_code=True, use_fast=False)
24
-
25
- model = AutoModelForSequenceClassification.from_pretrained(
26
- model_id,
27
- token=hf_token,
28
- device_map=device,
29
- trust_remote_code=True
30
- )
31
-
32
- sentiment_model = pipeline(
33
- "text-classification",
34
- model=model,
35
- tokenizer=tokenizer,
36
- )
37
-
38
- label_mapping = {
39
- "LABEL_0": "Negative",
40
- "LABEL_1": "Neutral",
41
- "LABEL_2": "Positive"
42
- }
43
-
44
- print("Sentiment model loaded successfully.")
45
-
46
- except Exception as e:
47
- print(f"FATAL ERROR: Failed to load sentiment model during startup: {e}")
48
- raise e
49
-
50
- app = FastAPI(
51
- title="YouTube Sentiment Analysis API",
52
- description="API for analyzing sentiment of Telugu/English/Transliterated YouTube comments using a Hugging Face model.",
53
- version="1.0.0",
54
- )
55
-
56
- class TextInput(BaseModel):
57
- text: str
58
-
59
- class SentimentOutput(BaseModel):
60
- comment: str
61
- sentiment: str
62
- score: float | None = None
63
 
64
  @app.get("/")
65
- async def read_root():
66
- return {"message": "YouTube Sentiment Analysis API is running."}
67
-
68
- @app.post("/analyze/sentiment", response_model=SentimentOutput)
69
- async def analyze_single_sentiment(item: TextInput):
70
- if sentiment_model is None:
71
- raise HTTPException(status_code=500, detail="Sentiment model not loaded during startup.")
72
-
73
- comment_text = item.text
74
-
75
- if not comment_text or not comment_text.strip():
76
- raise HTTPException(status_code=400, detail="Input text cannot be empty or just whitespace.")
77
-
78
- try:
79
- output = sentiment_model(comment_text)
80
-
81
- if output and isinstance(output, list) and isinstance(output[0], dict) and 'label' in output[0]:
82
- label = label_mapping.get(output[0]['label'], output[0]['label'])
83
- score = output[0].get('score', None)
84
-
85
- return SentimentOutput(
86
- comment=comment_text,
87
- sentiment=label,
88
- score=score
89
- )
90
- else:
91
- raise HTTPException(status_code=500, detail="Analysis output format unexpected from model pipeline.")
92
-
93
- except Exception as e:
94
- print(f"Error during sentiment analysis for text: '{comment_text[:50]}...' Error: {e}")
95
- raise HTTPException(status_code=500, detail=f"Error during sentiment analysis: {e}")
 
1
+ from fastapi import FastAPI
2
+ import uvicorn
3
  from pydantic import BaseModel
4
+ from typing import List
 
 
5
  import torch
6
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
7
 
8
+ class CommentsInput(BaseModel):
9
+ comments: List[str]
10
 
11
+ model_name = "gajula21/youtube-sentiment-model-telugu"
12
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
13
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
14
+ model.eval()
15
 
16
+ label_mapping = {0: "Negative", 1: "Neutral", 2: "Positive"}
 
17
 
18
+ app = FastAPI()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  @app.get("/")
21
+ def read_root():
22
+ return {"message": "Hello, World!"}
23
+
24
+ @app.post("/sentiment")
25
+ def predict_sentiments(data: CommentsInput):
26
+ inputs = tokenizer(data.comments, return_tensors="pt", padding=True, truncation=True, max_length=256)
27
+ with torch.no_grad():
28
+ outputs = model(**inputs)
29
+ predictions = torch.argmax(outputs.logits, dim=1).tolist()
30
+ sentiments = [label_mapping[p] for p in predictions]
31
+ return {"sentiments": sentiments}