Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,7 +11,7 @@ app = FastAPI(title="StudyLoop API", description="Extract YouTube transcripts")
|
|
| 11 |
# Enable CORS for your frontend
|
| 12 |
app.add_middleware(
|
| 13 |
CORSMiddleware,
|
| 14 |
-
allow_origins=["*"],
|
| 15 |
allow_credentials=True,
|
| 16 |
allow_methods=["*"],
|
| 17 |
allow_headers=["*"],
|
|
@@ -20,12 +20,6 @@ app.add_middleware(
|
|
| 20 |
class YouTubeRequest(BaseModel):
|
| 21 |
url: str
|
| 22 |
|
| 23 |
-
class TranscriptResponse(BaseModel):
|
| 24 |
-
success: bool
|
| 25 |
-
transcript: Optional[str] = None
|
| 26 |
-
error: Optional[str] = None
|
| 27 |
-
video_id: Optional[str] = None
|
| 28 |
-
|
| 29 |
def extract_video_id(url: str) -> Optional[str]:
|
| 30 |
patterns = [
|
| 31 |
r'(?:youtube\.com\/watch\?v=)([^&]+)',
|
|
@@ -51,22 +45,21 @@ async def health():
|
|
| 51 |
async def get_transcript(request: YouTubeRequest):
|
| 52 |
video_id = extract_video_id(request.url)
|
| 53 |
if not video_id:
|
| 54 |
-
return
|
| 55 |
|
| 56 |
try:
|
| 57 |
transcript_list = YouTubeTranscriptApi.get_transcript(video_id, languages=['en'])
|
| 58 |
formatter = TextFormatter()
|
| 59 |
text = formatter.format_transcript(transcript_list)
|
| 60 |
-
return
|
| 61 |
except Exception as e:
|
| 62 |
-
# Try without language filter
|
| 63 |
try:
|
| 64 |
transcript_list = YouTubeTranscriptApi.get_transcript(video_id)
|
| 65 |
formatter = TextFormatter()
|
| 66 |
text = formatter.format_transcript(transcript_list)
|
| 67 |
-
return
|
| 68 |
except Exception as e2:
|
| 69 |
-
return
|
| 70 |
|
| 71 |
@app.post("/extract")
|
| 72 |
async def extract(request: YouTubeRequest):
|
|
|
|
| 11 |
# Enable CORS for your frontend
|
| 12 |
app.add_middleware(
|
| 13 |
CORSMiddleware,
|
| 14 |
+
allow_origins=["*"],
|
| 15 |
allow_credentials=True,
|
| 16 |
allow_methods=["*"],
|
| 17 |
allow_headers=["*"],
|
|
|
|
| 20 |
class YouTubeRequest(BaseModel):
|
| 21 |
url: str
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
def extract_video_id(url: str) -> Optional[str]:
|
| 24 |
patterns = [
|
| 25 |
r'(?:youtube\.com\/watch\?v=)([^&]+)',
|
|
|
|
| 45 |
async def get_transcript(request: YouTubeRequest):
|
| 46 |
video_id = extract_video_id(request.url)
|
| 47 |
if not video_id:
|
| 48 |
+
return {"success": False, "error": "Invalid YouTube URL", "transcript": None}
|
| 49 |
|
| 50 |
try:
|
| 51 |
transcript_list = YouTubeTranscriptApi.get_transcript(video_id, languages=['en'])
|
| 52 |
formatter = TextFormatter()
|
| 53 |
text = formatter.format_transcript(transcript_list)
|
| 54 |
+
return {"success": True, "transcript": text, "video_id": video_id}
|
| 55 |
except Exception as e:
|
|
|
|
| 56 |
try:
|
| 57 |
transcript_list = YouTubeTranscriptApi.get_transcript(video_id)
|
| 58 |
formatter = TextFormatter()
|
| 59 |
text = formatter.format_transcript(transcript_list)
|
| 60 |
+
return {"success": True, "transcript": text, "video_id": video_id}
|
| 61 |
except Exception as e2:
|
| 62 |
+
return {"success": False, "error": f"Could not fetch transcript: {str(e2)}", "transcript": None}
|
| 63 |
|
| 64 |
@app.post("/extract")
|
| 65 |
async def extract(request: YouTubeRequest):
|