pdf_audio / app.py
mobenta's picture
Update app.py
f76166d verified
raw
history blame
1.43 kB
import cohere
import gradio as gr
from pypdf import PdfReader
import os
from loguru import logger
import promptic
# Initialize Cohere client with your API key
cohere_client = cohere.Client(os.getenv("vSS2Z6Jw3R73yh7XJpnZFttq1oTE0U94iFWdw6wG"))
# Function to extract text from PDF
def extract_text_from_pdf(pdf_file):
reader = PdfReader(pdf_file)
text = ""
for page in reader.pages:
text += page.extract_text()
return text
# Function to convert PDF text to audio via Cohere
def pdf_to_audio(pdf_file):
try:
text = extract_text_from_pdf(pdf_file)
# Generate response using Cohere
response = cohere_client.generate(
model='xlarge', # Change the model if necessary
prompt=text,
max_tokens=500 # Adjust based on your needs
)
generated_text = response.generations[0].text.strip()
# You could add audio generation code here or use text-to-speech libraries
return generated_text # Returning text for now
except Exception as e:
logger.error(f"Error during PDF to audio conversion: {e}")
return "An error occurred while processing the PDF."
# Gradio interface
def gradio_interface(pdf_file):
return pdf_to_audio(pdf_file)
# Launch the Gradio interface
gr.Interface(fn=gradio_interface, inputs="file", outputs="text", title="PDF to Audio using Cohere").launch()