-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
194 lines (132 loc) · 5.2 KB
/
app.py
File metadata and controls
194 lines (132 loc) · 5.2 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
from fastapi.middleware.cors import CORSMiddleware
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from db import db
from generateContent import generateHtmlContent
from generateVideo import generateVideos
from transcript import get_transcript
app = FastAPI()
origins = [
"http://localhost:3000", # Example: Your frontend on localhost
"https://sarfaraj.site", # Example: Your production frontend
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins, # Allows specific origins
allow_credentials=True, # Allows cookies to be included in requests
allow_methods=["*"], # Allows all HTTP methods (GET, POST, etc.)
allow_headers=["*"], # Allows all headers
)
@app.get("/htmlcontent/{videoId}")
def html_content(videoId: str):
print(videoId)
if not videoId:
return HTTPException(status_code=400, detail="Topic missing")
data = db["html_content"].find({'videoId': videoId}, {'_id': 0})
return list(data)
@app.get('/transcript/{videoId}')
def get_transcript_api(videoId:str):
try:
return get_transcript(videoId)
except Exception as e:
# print(e)
return {"error": str(e)}
@app.post("/generatehtmlcontent")
def generate_html_content(body: dict):
topic = body.get("topic")
videoId = body.get("videoId")
print(topic,videoId)
if not topic or not videoId:
raise HTTPException(status_code=400, detail="Topic missing")
video = db["videos"].find_one({'videoId': videoId})
if not video:
raise HTTPException(status_code=404, detail="Video not found")
content = [content for content in video["concept"] if content.get('concept description') == topic or content.get('description') == topic]
if len(content) == 0:
print("Content not found")
raise HTTPException(status_code=404, detail="Content not found")
html_content = generateHtmlContent(topic=topic).strip()
db["html_content"].insert_one({
"topic": topic,
"videoId": videoId,
"html_content": html_content
})
return {
"topic": topic,
"videoId": videoId,
"html_content": html_content
}
@app.get("/video/{videoId}")
def get_video(videoId: str):
print(videoId)
data = db["videos"].find_one({'videoId': videoId}, {"transcript": 0, '_id': 0})
if not data:
return HTTPException(status_code=404, detail="Video not found")
return data
@app.get("/videos/{subject}")
def get_videos(subject: str):
print(subject)
if not subject:
return HTTPException(status_code=400, detail="Subject missing")
data = db["videos"].find({'topic': subject}, {"transcript": 0, '_id': 0})
data = list(data)
return {
"videos": data
}
@app.post("/generatevideo")
def generate_video(body:dict):
try:
subject=body.get('subject')
if not subject:
return HTTPException(status_code=400, detail="Subject missing")
subjectData = db["subject"].find_one({"fields": subject})
if not subjectData:
return HTTPException(status_code=400, detail="Invalid topic")
course_name = subjectData.get("name") or ""
next_page_token_data = db["page_token"].find_one({"subject":subject})
if next_page_token_data:
next_page_token = next_page_token_data['token']
else:
next_page_token=None
query = f'"{subject}" AND "{course_name}" AND ("tutorial" OR "lesson" OR "lecture" OR "Explained" OR "one shot" OR "chapter" OR "learn")'
generatedData = generateVideos(query,4,next_page_token,course_name)
if not generatedData:
return HTTPException(status_code=400, detail="Rate limit exists, please try again later")
videos = generatedData.get('videos')
next_page_token = generatedData.get("nextPageToken")
if len(videos)==0:
return HTTPException(status_code=500, detail="Could not generate video")
db['page_token'].update_one({'subject':subject},{'$set':{'token':next_page_token}},True)
ids = [video['videoId'] for video in videos]
existing_videos = db["videos"].find({'videoId': {'$in': ids}}, {'videoId': 1})
existing_videos = [video['videoId'] for video in existing_videos]
filtered_videos = [video for video in videos if video['videoId'] not in existing_videos]
for v in filtered_videos:
v['topic'] = subject
v['subject'] = course_name
db["videos"].insert_many(filtered_videos)
videos = list(filtered_videos)
for item in videos:
if "_id" in item:
del item["_id"]
if "transcript" in item:
del item["transcript"]
print("Number of videos generated", len(videos))
return {
"videos": videos,
}
except Exception as e:
print(e)
return {"error": str(e)}
@app.get("/subjects")
def get_subjects():
data = db["subject"].find({}, {"_id": 0})
data = list(data)
return {
"subjects": data
}
# Run the application
if __name__ == '__main__':
import uvicorn
# uvicorn.run(app)
uvicorn.run(app,host="0.0.0.0",port=80)