Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,177 +3,174 @@ import google.generativeai as genai
|
|
| 3 |
from elevenlabs.client import ElevenLabs
|
| 4 |
import os
|
| 5 |
import json
|
| 6 |
-
import time
|
| 7 |
from pypdf import PdfReader
|
| 8 |
from dotenv import load_dotenv
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
| 11 |
load_dotenv()
|
| 12 |
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 13 |
ELEVEN_API_KEY = os.getenv("ELEVEN_API_KEY")
|
| 14 |
|
| 15 |
-
# 2. Configure APIs
|
| 16 |
if GEMINI_API_KEY:
|
| 17 |
genai.configure(api_key=GEMINI_API_KEY)
|
| 18 |
-
if ELEVEN_API_KEY:
|
| 19 |
-
client = ElevenLabs(api_key=ELEVEN_API_KEY)
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
class PodcastState:
|
| 23 |
def __init__(self):
|
| 24 |
self.script = []
|
| 25 |
-
self.
|
| 26 |
-
self.persona = "
|
| 27 |
self.full_text = ""
|
| 28 |
|
| 29 |
state = PodcastState()
|
| 30 |
|
| 31 |
-
# 4. Helper Functions
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
| 35 |
try:
|
| 36 |
-
reader = PdfReader(
|
| 37 |
text = ""
|
| 38 |
-
# Read first 5 pages max to save tokens for demo
|
| 39 |
for page in reader.pages[:5]:
|
| 40 |
text += page.extract_text() + "\n"
|
| 41 |
return text
|
| 42 |
except Exception as e:
|
| 43 |
return f"Error reading PDF: {e}"
|
| 44 |
|
| 45 |
-
|
|
|
|
| 46 |
if not pdf_file:
|
| 47 |
-
return "⚠️
|
| 48 |
-
|
| 49 |
-
if not GEMINI_API_KEY or not ELEVEN_API_KEY:
|
| 50 |
-
return "⚠️ API Keys missing! Check Settings -> Secrets.", []
|
| 51 |
|
| 52 |
-
# Read PDF
|
| 53 |
pdf_text = extract_text_from_pdf(pdf_file)
|
| 54 |
state.full_text = pdf_text
|
| 55 |
-
state.persona =
|
| 56 |
-
|
| 57 |
-
model = genai.GenerativeModel(
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
"Serious Academic": "Two professors discussing the paper.
|
| 61 |
-
"Gossip Columnist": "Two
|
| 62 |
-
"Explain Like I'm 5": "A gentle teacher and a curious student. Tone: Simple analogies, enthusiastic."
|
| 63 |
}
|
| 64 |
-
|
| 65 |
system_prompt = f"""
|
| 66 |
-
{
|
| 67 |
-
|
| 68 |
-
Based on the
|
| 69 |
-
"{pdf_text[:4000]}
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
RETURN RAW JSON ONLY. No markdown formatting. Format:
|
| 73 |
[
|
| 74 |
{{"speaker": "Host A", "text": "..."}},
|
| 75 |
{{"speaker": "Host B", "text": "..."}}
|
| 76 |
]
|
| 77 |
"""
|
| 78 |
-
|
| 79 |
try:
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
state.script =
|
| 85 |
-
state.
|
| 86 |
-
|
| 87 |
-
return "✅ Script
|
|
|
|
| 88 |
except Exception as e:
|
| 89 |
-
return f"Error: {
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
| 103 |
|
| 104 |
try:
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
model="eleven_monolingual_v1"
|
| 109 |
-
)
|
| 110 |
-
|
| 111 |
-
save_path = f"temp_{state.current_index}.mp3"
|
| 112 |
-
with open(save_path, "wb") as f:
|
| 113 |
-
for chunk in audio_stream:
|
| 114 |
f.write(chunk)
|
| 115 |
-
|
| 116 |
-
state.
|
| 117 |
-
return
|
|
|
|
| 118 |
except Exception as e:
|
| 119 |
-
return None, f"Audio
|
|
|
|
| 120 |
|
| 121 |
-
def
|
| 122 |
if not state.full_text:
|
| 123 |
return None, "Upload a PDF first."
|
| 124 |
|
| 125 |
-
model = genai.GenerativeModel(
|
| 126 |
-
|
| 127 |
prompt = f"""
|
| 128 |
-
You are a podcast host ({state.persona}).
|
| 129 |
Context: {state.full_text[:1000]}
|
| 130 |
-
User
|
| 131 |
-
|
| 132 |
-
1. Answer the question
|
| 133 |
2. Say "Anyway, back to the paper..."
|
| 134 |
"""
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
model="eleven_monolingual_v1"
|
| 143 |
-
)
|
| 144 |
-
|
| 145 |
-
save_path = "interrupt.mp3"
|
| 146 |
-
with open(save_path, "wb") as f:
|
| 147 |
-
for chunk in audio_stream:
|
| 148 |
f.write(chunk)
|
| 149 |
-
|
| 150 |
-
return
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from elevenlabs.client import ElevenLabs
|
| 4 |
import os
|
| 5 |
import json
|
|
|
|
| 6 |
from pypdf import PdfReader
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
|
| 9 |
+
|
| 10 |
+
# ---------------------------------------
|
| 11 |
+
# Load API Keys
|
| 12 |
+
# ---------------------------------------
|
| 13 |
load_dotenv()
|
| 14 |
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 15 |
ELEVEN_API_KEY = os.getenv("ELEVEN_API_KEY")
|
| 16 |
|
|
|
|
| 17 |
if GEMINI_API_KEY:
|
| 18 |
genai.configure(api_key=GEMINI_API_KEY)
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
client = ElevenLabs(api_key=ELEVEN_API_KEY) if ELEVEN_API_KEY else None
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
# ---------------------------------------
|
| 24 |
+
# Podcast State
|
| 25 |
+
# ---------------------------------------
|
| 26 |
class PodcastState:
|
| 27 |
def __init__(self):
|
| 28 |
self.script = []
|
| 29 |
+
self.index = 0
|
| 30 |
+
self.persona = ""
|
| 31 |
self.full_text = ""
|
| 32 |
|
| 33 |
state = PodcastState()
|
| 34 |
|
|
|
|
| 35 |
|
| 36 |
+
# ---------------------------------------
|
| 37 |
+
# Helpers
|
| 38 |
+
# ---------------------------------------
|
| 39 |
+
def extract_text_from_pdf(file):
|
| 40 |
try:
|
| 41 |
+
reader = PdfReader(file)
|
| 42 |
text = ""
|
|
|
|
| 43 |
for page in reader.pages[:5]:
|
| 44 |
text += page.extract_text() + "\n"
|
| 45 |
return text
|
| 46 |
except Exception as e:
|
| 47 |
return f"Error reading PDF: {e}"
|
| 48 |
|
| 49 |
+
|
| 50 |
+
def generate_script(pdf_file, persona):
|
| 51 |
if not pdf_file:
|
| 52 |
+
return "⚠️ Upload a PDF first.", []
|
|
|
|
|
|
|
|
|
|
| 53 |
|
|
|
|
| 54 |
pdf_text = extract_text_from_pdf(pdf_file)
|
| 55 |
state.full_text = pdf_text
|
| 56 |
+
state.persona = persona
|
| 57 |
+
|
| 58 |
+
model = genai.GenerativeModel("gemini-2.0-flash")
|
| 59 |
+
|
| 60 |
+
persona_prompts = {
|
| 61 |
+
"Serious Academic": "Two intelligent professors discussing the paper.",
|
| 62 |
+
"Gossip Columnist": "Two gossip hosts reacting dramatically.",
|
|
|
|
| 63 |
}
|
| 64 |
+
|
| 65 |
system_prompt = f"""
|
| 66 |
+
{persona_prompts.get(persona)}
|
| 67 |
+
|
| 68 |
+
Based on the text:
|
| 69 |
+
"{pdf_text[:4000]}"
|
| 70 |
+
|
| 71 |
+
Produce raw JSON only:
|
|
|
|
| 72 |
[
|
| 73 |
{{"speaker": "Host A", "text": "..."}},
|
| 74 |
{{"speaker": "Host B", "text": "..."}}
|
| 75 |
]
|
| 76 |
"""
|
| 77 |
+
|
| 78 |
try:
|
| 79 |
+
res = model.generate_content(system_prompt)
|
| 80 |
+
clean = res.text.replace("```json", "").replace("```", "")
|
| 81 |
+
script = json.loads(clean)
|
| 82 |
+
|
| 83 |
+
state.script = script
|
| 84 |
+
state.index = 0
|
| 85 |
+
|
| 86 |
+
return "✅ Script generated!", script
|
| 87 |
+
|
| 88 |
except Exception as e:
|
| 89 |
+
return f"Error: {e}", []
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def play_next():
|
| 93 |
+
if state.index >= len(state.script):
|
| 94 |
+
return None, "🎉 Podcast complete."
|
| 95 |
+
|
| 96 |
+
line = state.script[state.index]
|
| 97 |
+
text = line["text"]
|
| 98 |
+
|
| 99 |
+
# choose voice
|
| 100 |
+
voice = "nPczCjz82tPNOwVbpGE2" # default
|
| 101 |
+
if state.persona == "Gossip Columnist":
|
| 102 |
+
voice = "EXAVITQu4vr4xnSDxMaL" if line["speaker"] == "Host B" else voice
|
| 103 |
+
|
| 104 |
+
audio_path = f"audio_{state.index}.mp3"
|
| 105 |
|
| 106 |
try:
|
| 107 |
+
stream = client.generate(text=text, voice=voice, model="eleven_monolingual_v1")
|
| 108 |
+
with open(audio_path, "wb") as f:
|
| 109 |
+
for chunk in stream:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
f.write(chunk)
|
| 111 |
+
|
| 112 |
+
state.index += 1
|
| 113 |
+
return audio_path, f"🎙️ {line['speaker']}: {text}"
|
| 114 |
+
|
| 115 |
except Exception as e:
|
| 116 |
+
return None, f"Audio error: {e}"
|
| 117 |
+
|
| 118 |
|
| 119 |
+
def interrupt(question):
|
| 120 |
if not state.full_text:
|
| 121 |
return None, "Upload a PDF first."
|
| 122 |
|
| 123 |
+
model = genai.GenerativeModel("gemini-2.0-flash")
|
| 124 |
+
|
| 125 |
prompt = f"""
|
| 126 |
+
You are a podcast host ({state.persona}).
|
| 127 |
Context: {state.full_text[:1000]}
|
| 128 |
+
User asked: "{question}"
|
| 129 |
+
|
| 130 |
+
1. Answer the question.
|
| 131 |
2. Say "Anyway, back to the paper..."
|
| 132 |
"""
|
| 133 |
+
|
| 134 |
+
ans = model.generate_content(prompt).text
|
| 135 |
+
|
| 136 |
+
audio_path = "interrupt.mp3"
|
| 137 |
+
stream = client.generate(text=ans, voice="nPczCjz82tPNOwVbpGE2", model="eleven_monolingual_v1")
|
| 138 |
+
with open(audio_path, "wb") as f:
|
| 139 |
+
for chunk in stream:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
f.write(chunk)
|
| 141 |
+
|
| 142 |
+
return audio_path, ans
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
# ---------------------------------------
|
| 146 |
+
# GRADIO 6 APP
|
| 147 |
+
# ---------------------------------------
|
| 148 |
+
with gr.App() as app:
|
| 149 |
+
|
| 150 |
+
gr.Markdown("# 🎧 PodQuery (Gradio 6 Edition)")
|
| 151 |
+
|
| 152 |
+
pdf_input = gr.File(label="Upload PDF")
|
| 153 |
+
persona = gr.Dropdown(
|
| 154 |
+
["Serious Academic", "Gossip Columnist"],
|
| 155 |
+
value="Serious Academic",
|
| 156 |
+
label="Persona"
|
| 157 |
+
)
|
| 158 |
+
btn_gen = gr.Button("Generate Script")
|
| 159 |
+
status = gr.Textbox(label="Status")
|
| 160 |
+
script_box = gr.JSON(label="Generated Script")
|
| 161 |
+
|
| 162 |
+
player = gr.Audio(label="Audio", autoplay=True)
|
| 163 |
+
transcript = gr.Textbox(label="Transcript")
|
| 164 |
+
|
| 165 |
+
btn_play = gr.Button("▶️ Play Next Line")
|
| 166 |
+
|
| 167 |
+
q_input = gr.Textbox(label="Interrupt question")
|
| 168 |
+
btn_interrupt = gr.Button("✋ Interrupt Podcast")
|
| 169 |
+
|
| 170 |
+
# wiring events
|
| 171 |
+
btn_gen.subscribe(generate_script, [pdf_input, persona], [status, script_box])
|
| 172 |
+
btn_play.subscribe(play_next, [], [player, transcript])
|
| 173 |
+
btn_interrupt.subscribe(interrupt, [q_input], [player, transcript])
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
app.launch()
|