Spaces:
Running
Running
File size: 2,199 Bytes
2460f2c a75c882 2460f2c a75c882 2460f2c a972780 2460f2c a972780 2460f2c a972780 2460f2c a972780 2460f2c a972780 |
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
import gradio as gr
from groq import Groq
from PyPDF2 import PdfReader
from docx import Document
import os
from gtts import gTTS
import tempfile
import whisper
# Load Groq API key from environment variable
groq_api_key = os.getenv('GROQ_API_KEY')
if not groq_api_key:
raise ValueError("GROQ_API_KEY environment variable is not set.")
groq_client = Groq(api_key=groq_api_key)
# Load Whisper model
model = whisper.load_model("base")
def summarize_document(file):
# Read file content
if file.name.endswith('.pdf'):
# Read PDF file
reader = PdfReader(file.name)
text = ''.join([page.extract_text() for page in reader.pages])
elif file.name.endswith('.docx'):
# Read DOCX file
doc = Document(file.name)
text = ''.join([para.text for para in doc.paragraphs])
else:
return "Unsupported file format. Please upload a PDF or DOCX file.", None
# Generate summary
try:
chat_completion = groq_client.chat.completions.create(
messages=[
{"role": "user", "content": f"Please summarize the following text: {text}"}
],
model="llama3-8b-8192",
)
summary = chat_completion.choices[0].message.content
except Exception as e:
return f"Error generating summary: {e}", None
# Convert summary text to speech using gTTS
try:
tts = gTTS(text=summary, lang='en')
audio_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp3')
tts.save(audio_file.name)
audio_file.close()
except Exception as e:
return f"Error generating audio: {e}", None
return summary, audio_file.name
# Create Gradio interface with Monochrome theme
iface = gr.Interface(
fn=summarize_document,
inputs=gr.File(label="Upload a Word or PDF Document"),
outputs=[gr.Textbox(label="Summary"), gr.Audio(label="Audio Summary")],
title="Document Summarizer",
description="Upload a Word or PDF document and get a summary with audio playback.",
theme=gr.themes.Monochrome()
)
# Launch the interface with sharing enabled
iface.launch(share=False, debug=True) # Use `share=True` if deploying elsewhere
|