Spaces:
Sleeping
Sleeping
from youtube_transcript_api import YouTubeTranscriptApi | |
from constants import OPENAI_API_KEY, OPENAI_API_BASE_URL, OPEN_AI_MODEL | |
import json | |
import requests | |
def create_transcript_from_youtube_api(youtube_video_id): | |
if not youtube_video_id: | |
return {"success": False, "error": "Please pass your youtube video-id"} | |
try: | |
list_transcript = YouTubeTranscriptApi.get_transcript(youtube_video_id) | |
transcript = ' '.join(entry['text'] for entry in list_transcript) | |
return {"success": True, "data": transcript} | |
except Exception as e: | |
return {"success": False, "error": f"Transcription failed due to : {e}"} | |
def create_open_ai_query(input_query, system_message=None, model_engine=OPEN_AI_MODEL): | |
openai_url = f"{OPENAI_API_BASE_URL}/chat/completions" | |
headers = {'Authorization': f'Bearer {OPENAI_API_KEY}', 'Content-Type': 'application/json'} | |
messages = [] | |
if system_message: | |
messages.append({"role": "system", "content": system_message}) | |
messages.append({"role": "user", "content": input_query}) | |
payload = { | |
'model': model_engine, | |
'messages': messages | |
} | |
response = requests.post(openai_url, headers=headers, data=json.dumps(payload)) | |
if response.status_code == 200 and 'choices' in response.json(): | |
content_text = response.json()['choices'][0]['message']['content'].strip() | |
return {"success": True, "data": content_text, "response_json": response.json()} | |
return {"success": False, "error": response.text} |