Spaces:
Runtime error

gajula21 commited on
Commit
b430bb0
·
2 Parent(s): d551cf8c17b19f

Merge branch 'main' of https://huggingface.co/spaces/gajula21/telugu-sentiment-api

Browse files
Files changed (3) hide show
  1. README.md +1 -1
  2. main.py +0 -94
  3. requirement.txt +0 -6
README.md CHANGED
@@ -3,7 +3,7 @@ title: Telugu Sentiment Api
3
  emoji: 📊
4
  colorFrom: pink
5
  colorTo: pink
6
- sdk: docker
7
  pinned: false
8
  license: apache-2.0
9
  ---
 
3
  emoji: 📊
4
  colorFrom: pink
5
  colorTo: pink
6
+ sdk: docker
7
  pinned: false
8
  license: apache-2.0
9
  ---
main.py DELETED
@@ -1,94 +0,0 @@
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)
24
-
25
- model = AutoModelForSequenceClassification.from_pretrained(
26
- model_id,
27
- token=hf_token,
28
- device_map=device
29
- )
30
-
31
- sentiment_model = pipeline(
32
- "text-classification",
33
- model=model,
34
- tokenizer=tokenizer,
35
- )
36
-
37
- label_mapping = {
38
- "LABEL_0": "Negative",
39
- "LABEL_1": "Neutral",
40
- "LABEL_2": "Positive"
41
- }
42
-
43
- print("Sentiment model loaded successfully.")
44
-
45
- except Exception as e:
46
- print(f"FATAL ERROR: Failed to load sentiment model during startup: {e}")
47
- raise e
48
-
49
- app = FastAPI(
50
- title="YouTube Sentiment Analysis API",
51
- description="API for analyzing sentiment of Telugu/English/Transliterated YouTube comments using a Hugging Face model.",
52
- version="1.0.0",
53
- )
54
-
55
- class TextInput(BaseModel):
56
- text: str
57
-
58
- class SentimentOutput(BaseModel):
59
- comment: str
60
- sentiment: str
61
- score: float | None = None
62
-
63
- @app.get("/")
64
- async def read_root():
65
- return {"message": "YouTube Sentiment Analysis API is running."}
66
-
67
- @app.post("/analyze/sentiment", response_model=SentimentOutput)
68
- async def analyze_single_sentiment(item: TextInput):
69
- if sentiment_model is None:
70
- raise HTTPException(status_code=500, detail="Sentiment model not loaded during startup.")
71
-
72
- comment_text = item.text
73
-
74
- if not comment_text or not comment_text.strip():
75
- raise HTTPException(status_code=400, detail="Input text cannot be empty or just whitespace.")
76
-
77
- try:
78
- output = sentiment_model(comment_text)
79
-
80
- if output and isinstance(output, list) and isinstance(output[0], dict) and 'label' in output[0]:
81
- label = label_mapping.get(output[0]['label'], output[0]['label'])
82
- score = output[0].get('score', None)
83
-
84
- return SentimentOutput(
85
- comment=comment_text,
86
- sentiment=label,
87
- score=score
88
- )
89
- else:
90
- raise HTTPException(status_code=500, detail="Analysis output format unexpected from model pipeline.")
91
-
92
- except Exception as e:
93
- print(f"Error during sentiment analysis for text: '{comment_text[:50]}...' Error: {e}")
94
- raise HTTPException(status_code=500, detail=f"Error during sentiment analysis: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
requirement.txt DELETED
@@ -1,6 +0,0 @@
1
- fastapi
2
- uvicorn
3
- python-dotenv
4
- transformers
5
- torch
6
- pandas