ibrahim313 commited on
Commit
2460f2c
·
verified ·
1 Parent(s): bbcbb9e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -0
app.py CHANGED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from groq import Groq
3
+ from PyPDF2 import PdfReader
4
+ from docx import Document
5
+ import os
6
+ from gtts import gTTS
7
+ import tempfile
8
+ import whisper
9
+
10
+ # Set the API key for Groq
11
+ api_key = "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"
12
+ os.environ["GROQ_API_KEY"] = api_key
13
+
14
+ # Initialize Groq client with API key from environment variable
15
+ api_key = os.getenv("GROQ_API_KEY")
16
+ if not api_key:
17
+ raise ValueError("API key for Groq is not set. Please set the environment variable GROQ_API_KEY.")
18
+
19
+ client = Groq(api_key=api_key)
20
+
21
+ # Load Whisper model
22
+ model = whisper.load_model("base")
23
+
24
+ def summarize_document(file):
25
+ # Read file content
26
+ if file.name.endswith('.pdf'):
27
+ # Read PDF file
28
+ reader = PdfReader(file.name)
29
+ text = ''.join([page.extract_text() for page in reader.pages])
30
+ elif file.name.endswith('.docx'):
31
+ # Read DOCX file
32
+ doc = Document(file.name)
33
+ text = ''.join([para.text for para in doc.paragraphs])
34
+ else:
35
+ return "Unsupported file format. Please upload a PDF or DOCX file."
36
+
37
+ # Generate summary
38
+ chat_completion = client.chat.completions.create(
39
+ messages=[
40
+ {"role": "user", "content": f"Please summarize the following text: {text}"}
41
+ ],
42
+ model="llama3-8b-8192",
43
+ )
44
+
45
+ summary = chat_completion.choices[0].message.content
46
+
47
+ # Convert summary text to speech using gTTS
48
+ tts = gTTS(text=summary, lang='en')
49
+ audio_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp3')
50
+ tts.save(audio_file.name)
51
+
52
+ return summary, audio_file.name
53
+
54
+ # Create Gradio interface with Monochrome theme
55
+ iface = gr.Interface(
56
+ fn=summarize_document,
57
+ inputs=gr.File(label="Upload a Word or PDF Document"),
58
+ outputs=[gr.Textbox(label="Summary"), gr.Audio(label="Audio Summary")],
59
+ title="Document Summarizer",
60
+ description="Upload a Word or PDF document and get a summary with audio playback.",
61
+ theme=gr.themes.Monochrome() # Apply Monochrome theme
62
+ )
63
+
64
+ # Launch the interface with sharing enabled
65
+ iface.launch(share=True, debug=True)