-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
88 lines (74 loc) · 2.71 KB
/
main.py
File metadata and controls
88 lines (74 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from fastapi import FastAPI
from pydantic import BaseModel
from fastapi.middleware.cors import CORSMiddleware
import torch
import warnings
import tiktoken
from scripts.youtube_data_fetcher import YouTubeDataFetcher
from scripts.pre_processes import Utility as ut
from backend.models.Model.GPT_2 import GPTModel, GPT_CONFIG_124M
warnings.filterwarnings('ignore')
# Initialize FastAPI app
app = FastAPI()
tokenizer = tiktoken.get_encoding("gpt2")
# Enable CORS for frontend integration
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Load model and tokenizer
API_KEY = ut.get_api_key()
device = "cpu"
# Load the GPT Model
try:
gpt = GPTModel(GPT_CONFIG_124M)
gpt.eval()
optimizer = torch.optim.Adam(gpt.parameters(), lr=0.0004, weight_decay=0.1)
# Load model weights (ensure the path is correct)
checkpoint_path = "backend/models/Model Weights/model_and_optimizer_youtubes_senti.pth"
checkpoint = torch.load(checkpoint_path, map_location=device)
gpt.load_state_dict(checkpoint["model_state_dict"])
optimizer.load_state_dict(checkpoint["optimizer_state_dict"])
except Exception as e:
print(f"❌ Error loading model: {e}")
gpt = None
# Define the VideoLink model for API request
class VideoLink(BaseModel):
link: str
@app.get("/")
def home():
"""Health check endpoint"""
return {"message": "YouTube Sentiment Analysis API is running!"}
@app.post("/analyze_comments/")
def analyze_comments(video: VideoLink):
"""Analyze sentiment of comments for a given YouTube video"""
if gpt is None:
return {"error": "Model not loaded properly"}
try:
# Extract video ID from the YouTube link
video_id = ut.extract_video_id(video.link)
# Fetch YouTube data
fetcher = YouTubeDataFetcher(api_key=API_KEY, video_id=video_id)
data = fetcher.fetch_youtube_data()
# Extract video details
title = data.get("Title", "Unknown")
publish_date = data.get("Publish Date", "Unknown")
views = data.get("Views", "Unknown")
top_comments = data.get("Top Comments", [])
total_comments = len(data.get("All Comments", []))
# Analyze sentiment of comments
sentiment_counts = ut.analyze_sentiments_all_comments(data=data, model=gpt, device=device,tokenizer=tokenizer)
return {
"video_id": video_id,
"title": title,
"publish_date": publish_date,
"views": views,
"total_comments": total_comments,
"top_comments": top_comments,
"sentiment_counts": sentiment_counts
}
except Exception as e:
return {"error": str(e)}