Spaces:
Running
Running
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 | |