Spaces:
Sleeping
Sleeping
Update src/mainFunctions.py
Browse files- src/mainFunctions.py +0 -111
src/mainFunctions.py
CHANGED
|
@@ -33,114 +33,3 @@ def parse_quiz_content(quiz_text):
|
|
| 33 |
questions.append(current_question)
|
| 34 |
|
| 35 |
return {"questions": questions}
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
def transcribe_audio(audio_path, elevenlabs_api_key, model_id):
|
| 39 |
-
import requests
|
| 40 |
-
import json
|
| 41 |
-
|
| 42 |
-
try:
|
| 43 |
-
with open(audio_path, 'rb') as audio_file:
|
| 44 |
-
response = requests.post(
|
| 45 |
-
'https://api.elevenlabs.io/v1/transcribe',
|
| 46 |
-
headers={'xi-api-key': elevenlabs_api_key},
|
| 47 |
-
files={'audio': audio_file},
|
| 48 |
-
data={'model_id': model_id}
|
| 49 |
-
)
|
| 50 |
-
|
| 51 |
-
if response.status_code == 200:
|
| 52 |
-
transcription = response.json().get('transcription', '')
|
| 53 |
-
|
| 54 |
-
transcript_path = tempfile.mktemp(suffix='.txt')
|
| 55 |
-
with open(transcript_path, 'w', encoding='utf-8') as f:
|
| 56 |
-
f.write(transcription)
|
| 57 |
-
|
| 58 |
-
return transcription, transcript_path, "Transcription completed successfully"
|
| 59 |
-
else:
|
| 60 |
-
return None, None, f"Transcription failed: {response.text}"
|
| 61 |
-
except Exception as e:
|
| 62 |
-
return None, None, f"Transcription error: {str(e)}"
|
| 63 |
-
|
| 64 |
-
def process_video_file(video_path, audio_format, elevenlabs_api_key, model_id, gemini_api_key, language, content_type):
|
| 65 |
-
try:
|
| 66 |
-
audio_path = extract_audio_from_video(video_path, audio_format)
|
| 67 |
-
|
| 68 |
-
transcription, transcript_path, transcription_status = transcribe_audio(
|
| 69 |
-
audio_path,
|
| 70 |
-
elevenlabs_api_key,
|
| 71 |
-
model_id
|
| 72 |
-
)
|
| 73 |
-
|
| 74 |
-
if not transcription:
|
| 75 |
-
return audio_path, "Audio extracted, but transcription failed", None, transcription_status, None, None, None
|
| 76 |
-
|
| 77 |
-
formatted_output, json_path, txt_path = analyze_document(
|
| 78 |
-
transcription,
|
| 79 |
-
gemini_api_key,
|
| 80 |
-
language,
|
| 81 |
-
content_type
|
| 82 |
-
)
|
| 83 |
-
|
| 84 |
-
return audio_path, "Processing completed successfully", transcript_path, transcription_status, formatted_output, txt_path, json_path
|
| 85 |
-
except Exception as e:
|
| 86 |
-
error_message = f"Error processing video: {str(e)}"
|
| 87 |
-
return None, error_message, None, error_message, error_message, None, None
|
| 88 |
-
|
| 89 |
-
def process_youtube_video(youtube_url, audio_format, elevenlabs_api_key, model_id, gemini_api_key, language, content_type):
|
| 90 |
-
try:
|
| 91 |
-
yt = pytube.YouTube(youtube_url)
|
| 92 |
-
stream = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first()
|
| 93 |
-
|
| 94 |
-
if not stream:
|
| 95 |
-
raise Exception("No suitable video stream found")
|
| 96 |
-
|
| 97 |
-
# Download to temporary file
|
| 98 |
-
video_path = tempfile.mktemp(suffix='.mp4')
|
| 99 |
-
stream.download(filename=video_path)
|
| 100 |
-
audio_path = extract_audio_from_video(video_path, audio_format)
|
| 101 |
-
|
| 102 |
-
transcription, transcript_path, transcription_status = transcribe_audio(
|
| 103 |
-
audio_path,
|
| 104 |
-
elevenlabs_api_key,
|
| 105 |
-
model_id
|
| 106 |
-
)
|
| 107 |
-
|
| 108 |
-
if not transcription:
|
| 109 |
-
return audio_path, "Audio extracted, but transcription failed", None, transcription_status, None, None, None
|
| 110 |
-
|
| 111 |
-
formatted_output, json_path, txt_path = analyze_document(
|
| 112 |
-
transcription,
|
| 113 |
-
gemini_api_key,
|
| 114 |
-
language,
|
| 115 |
-
content_type
|
| 116 |
-
)
|
| 117 |
-
|
| 118 |
-
return audio_path, "Processing completed successfully", transcript_path, transcription_status, formatted_output, txt_path, json_path
|
| 119 |
-
except Exception as e:
|
| 120 |
-
error_message = f"Error processing YouTube video: {str(e)}"
|
| 121 |
-
return None, error_message, None, error_message, error_message, None, None
|
| 122 |
-
|
| 123 |
-
def process_audio_document(audio_path, elevenlabs_api_key, model_id, gemini_api_key, language, content_type):
|
| 124 |
-
"""Process an audio file - transcribe and generate summary or quiz."""
|
| 125 |
-
try:
|
| 126 |
-
transcription, transcript_path, transcription_status = transcribe_audio(
|
| 127 |
-
audio_path,
|
| 128 |
-
elevenlabs_api_key,
|
| 129 |
-
model_id
|
| 130 |
-
)
|
| 131 |
-
|
| 132 |
-
if not transcription:
|
| 133 |
-
return "Transcription failed", None, None, None, None
|
| 134 |
-
|
| 135 |
-
formatted_output, json_path, txt_path = analyze_document(
|
| 136 |
-
transcription,
|
| 137 |
-
gemini_api_key,
|
| 138 |
-
language,
|
| 139 |
-
content_type
|
| 140 |
-
)
|
| 141 |
-
|
| 142 |
-
return "Processing completed successfully", transcript_path, formatted_output, txt_path, json_path
|
| 143 |
-
except Exception as e:
|
| 144 |
-
error_message = f"Error processing audio: {str(e)}"
|
| 145 |
-
return error_message, None, error_message, None, None
|
| 146 |
-
|
|
|
|
| 33 |
questions.append(current_question)
|
| 34 |
|
| 35 |
return {"questions": questions}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|