Sfh28450 commited on
Commit
71154d0
·
verified ·
1 Parent(s): 2771a53

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -93
app.py DELETED
@@ -1,93 +0,0 @@
1
- import os
2
- import yt_dlp
3
- import gradio as gr
4
- from faster_whisper import WhisperModel
5
- from transformers import pipeline, AutoModelForSeq2SeqLM, AutoTokenizer
6
- import openai
7
- import torch
8
-
9
- # Optional: Set your OpenAI API key (use env var for security)
10
- openai.api_key = "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" # Replace with your real key or set as os.environ["OPENAI_API_KEY"]
11
-
12
- def download_and_extract_audio(youtube_url):
13
- output_path = "downloads"
14
- os.makedirs(output_path, exist_ok=True)
15
-
16
- ydl_opts = {
17
- 'format': 'bestaudio/best',
18
- 'outtmpl': os.path.join(output_path, '%(id)s.%(ext)s'),
19
- 'postprocessors': [{
20
- 'key': 'FFmpegExtractAudio',
21
- 'preferredcodec': 'mp3',
22
- 'preferredquality': '192',
23
- }],
24
- }
25
-
26
- with yt_dlp.YoutubeDL(ydl_opts) as ydl:
27
- info_dict = ydl.extract_info(youtube_url, download=True)
28
- video_id = info_dict.get("id", None)
29
- filename = os.path.join(output_path, f"{video_id}.mp3")
30
- return filename
31
-
32
- def transcribe_audio(audio_path):
33
- model = WhisperModel("base", compute_type="int8", device="cuda" if torch.cuda.is_available() else "cpu")
34
- segments, _ = model.transcribe(audio_path)
35
- transcript = " ".join([seg.text for seg in segments])
36
- return transcript
37
-
38
- # Preload FLAN-T5 model offline
39
- tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-large")
40
- model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-large")
41
- local_gen = pipeline("text2text-generation", model=model, tokenizer=tokenizer)
42
-
43
- def generate_response(transcript, user_prompt, use_online=False):
44
- prompt = f"""You are a helpful AI assistant. Based on the transcript of a video, please {user_prompt.strip().lower()}.
45
-
46
- Transcript:
47
- {transcript[:3000]}"""
48
-
49
- if use_online:
50
- try:
51
- response = openai.ChatCompletion.create(
52
- model="gpt-4",
53
- messages=[{"role": "user", "content": prompt}],
54
- max_tokens=1000
55
- )
56
- return response.choices[0].message["content"]
57
- except Exception as e:
58
- return f"⚠️ Online API failed: {str(e)}"
59
- else:
60
- result = local_gen(prompt, max_length=1024, do_sample=False)
61
- return result[0]['generated_text']
62
-
63
- def enhanced_ai_study_pipeline(video_source, youtube_url, upload_file, user_prompt, use_online_api):
64
- try:
65
- if video_source == "YouTube URL":
66
- audio_path = download_and_extract_audio(youtube_url)
67
- elif video_source == "Upload File" and upload_file is not None:
68
- audio_path = upload_file.name
69
- else:
70
- return "No valid input provided.", ""
71
-
72
- transcript = transcribe_audio(audio_path)
73
- ai_response = generate_response(transcript, user_prompt, use_online=use_online_api)
74
- return transcript, ai_response
75
- except Exception as e:
76
- return "Error occurred", str(e)
77
-
78
- video_input = gr.Radio(["YouTube URL", "Upload File"], label="Video Source")
79
- youtube_url = gr.Textbox(label="Enter YouTube URL")
80
- upload_file = gr.File(label="Upload a Video File", file_types=[".mp4", ".mp3", ".wav"])
81
- user_prompt = gr.Textbox(label="What do you want from the transcript?", placeholder="e.g., Prepare a diet plan based on this video")
82
- use_online_api = gr.Checkbox(label="Use Online API (GPT-4)", value=False)
83
-
84
- gr.Interface(
85
- fn=enhanced_ai_study_pipeline,
86
- inputs=[video_input, youtube_url, upload_file, user_prompt, use_online_api],
87
- outputs=[
88
- gr.Textbox(label="Transcript"),
89
- gr.Textbox(label="AI Response")
90
- ],
91
- title="📚 AI Transcription Assistant (Offline + Online GPT)",
92
- description="Upload or paste a YouTube video. Enter your goal and get a smart AI answer. Works offline with FLAN-T5 or online with GPT-4."
93
- ).launch(share=True)