Upload 2 files
Browse files- app (1).py +45 -0
- requirements (6).txt +8 -0
app (1).py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import cohere
|
3 |
+
import gradio as gr
|
4 |
+
from pypdf import PdfReader
|
5 |
+
import os
|
6 |
+
from loguru import logger
|
7 |
+
import promptic
|
8 |
+
|
9 |
+
# Initialize Cohere client with your API key
|
10 |
+
cohere_client = cohere.Client(os.getenv("COHERE_API_KEY"))
|
11 |
+
|
12 |
+
# Function to extract text from PDF
|
13 |
+
def extract_text_from_pdf(pdf_file):
|
14 |
+
reader = PdfReader(pdf_file)
|
15 |
+
text = ""
|
16 |
+
for page in reader.pages:
|
17 |
+
text += page.extract_text()
|
18 |
+
return text
|
19 |
+
|
20 |
+
# Function to convert PDF text to audio via Cohere
|
21 |
+
def pdf_to_audio(pdf_file):
|
22 |
+
try:
|
23 |
+
text = extract_text_from_pdf(pdf_file)
|
24 |
+
|
25 |
+
# Generate response using Cohere
|
26 |
+
response = cohere_client.generate(
|
27 |
+
model='xlarge', # Change the model if necessary
|
28 |
+
prompt=text,
|
29 |
+
max_tokens=500 # Adjust based on your needs
|
30 |
+
)
|
31 |
+
generated_text = response.generations[0].text.strip()
|
32 |
+
|
33 |
+
# You could add audio generation code here or use text-to-speech libraries
|
34 |
+
|
35 |
+
return generated_text # Returning text for now
|
36 |
+
except Exception as e:
|
37 |
+
logger.error(f"Error during PDF to audio conversion: {e}")
|
38 |
+
return "An error occurred while processing the PDF."
|
39 |
+
|
40 |
+
# Gradio interface
|
41 |
+
def gradio_interface(pdf_file):
|
42 |
+
return pdf_to_audio(pdf_file)
|
43 |
+
|
44 |
+
# Launch the Gradio interface
|
45 |
+
gr.Interface(fn=gradio_interface, inputs="file", outputs="text", title="PDF to Audio using Cohere").launch()
|
requirements (6).txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
gradio
|
3 |
+
pandas
|
4 |
+
pypdf
|
5 |
+
loguru
|
6 |
+
promptic
|
7 |
+
tenacity
|
8 |
+
cohere
|