File size: 1,431 Bytes
53e0ff0
 
 
 
 
 
 
 
 
f76166d
53e0ff0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

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()