Spaces:
Sleeping
Sleeping
File size: 2,091 Bytes
2460f2c |
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 |
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
# Set the API key for Groq
api_key = "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"
os.environ["GROQ_API_KEY"] = api_key
# Initialize Groq client with API key from environment variable
api_key = os.getenv("GROQ_API_KEY")
if not api_key:
raise ValueError("API key for Groq is not set. Please set the environment variable GROQ_API_KEY.")
client = Groq(api_key=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."
# Generate summary
chat_completion = 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
# Convert summary text to speech using gTTS
tts = gTTS(text=summary, lang='en')
audio_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp3')
tts.save(audio_file.name)
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() # Apply Monochrome theme
)
# Launch the interface with sharing enabled
iface.launch(share=True, debug=True)
|