ibrahim313's picture
Update app.py
2460f2c verified
raw
history blame
2.09 kB
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)