Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,7 +14,6 @@ if not groq_api_key:
|
|
| 14 |
|
| 15 |
groq_client = Groq(api_key=groq_api_key)
|
| 16 |
|
| 17 |
-
|
| 18 |
# Load Whisper model
|
| 19 |
model = whisper.load_model("base")
|
| 20 |
|
|
@@ -29,22 +28,28 @@ def summarize_document(file):
|
|
| 29 |
doc = Document(file.name)
|
| 30 |
text = ''.join([para.text for para in doc.paragraphs])
|
| 31 |
else:
|
| 32 |
-
return "Unsupported file format. Please upload a PDF or DOCX file."
|
| 33 |
|
| 34 |
# Generate summary
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
| 43 |
|
| 44 |
# Convert summary text to speech using gTTS
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
return summary, audio_file.name
|
| 50 |
|
|
@@ -55,9 +60,8 @@ iface = gr.Interface(
|
|
| 55 |
outputs=[gr.Textbox(label="Summary"), gr.Audio(label="Audio Summary")],
|
| 56 |
title="Document Summarizer",
|
| 57 |
description="Upload a Word or PDF document and get a summary with audio playback.",
|
| 58 |
-
theme=gr.themes.Monochrome()
|
| 59 |
)
|
| 60 |
|
| 61 |
# Launch the interface with sharing enabled
|
| 62 |
-
iface.launch(share=
|
| 63 |
-
|
|
|
|
| 14 |
|
| 15 |
groq_client = Groq(api_key=groq_api_key)
|
| 16 |
|
|
|
|
| 17 |
# Load Whisper model
|
| 18 |
model = whisper.load_model("base")
|
| 19 |
|
|
|
|
| 28 |
doc = Document(file.name)
|
| 29 |
text = ''.join([para.text for para in doc.paragraphs])
|
| 30 |
else:
|
| 31 |
+
return "Unsupported file format. Please upload a PDF or DOCX file.", None
|
| 32 |
|
| 33 |
# Generate summary
|
| 34 |
+
try:
|
| 35 |
+
chat_completion = groq_client.chat.completions.create(
|
| 36 |
+
messages=[
|
| 37 |
+
{"role": "user", "content": f"Please summarize the following text: {text}"}
|
| 38 |
+
],
|
| 39 |
+
model="llama3-8b-8192",
|
| 40 |
+
)
|
| 41 |
+
summary = chat_completion.choices[0].message.content
|
| 42 |
+
except Exception as e:
|
| 43 |
+
return f"Error generating summary: {e}", None
|
| 44 |
|
| 45 |
# Convert summary text to speech using gTTS
|
| 46 |
+
try:
|
| 47 |
+
tts = gTTS(text=summary, lang='en')
|
| 48 |
+
audio_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp3')
|
| 49 |
+
tts.save(audio_file.name)
|
| 50 |
+
audio_file.close()
|
| 51 |
+
except Exception as e:
|
| 52 |
+
return f"Error generating audio: {e}", None
|
| 53 |
|
| 54 |
return summary, audio_file.name
|
| 55 |
|
|
|
|
| 60 |
outputs=[gr.Textbox(label="Summary"), gr.Audio(label="Audio Summary")],
|
| 61 |
title="Document Summarizer",
|
| 62 |
description="Upload a Word or PDF document and get a summary with audio playback.",
|
| 63 |
+
theme=gr.themes.Monochrome()
|
| 64 |
)
|
| 65 |
|
| 66 |
# Launch the interface with sharing enabled
|
| 67 |
+
iface.launch(share=False, debug=True) # Use `share=True` if deploying elsewhere
|
|
|