mrsk1883 commited on
Commit
46a9e4e
·
1 Parent(s): 0ae5e3c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -9
app.py CHANGED
@@ -1,10 +1,16 @@
1
  import gradio as gr
2
-
3
  from PyPDF2 import PdfReader
4
  from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
5
  from gtts import gTTS
6
  from io import BytesIO
7
 
 
 
 
 
 
 
 
8
  # Define model and tokenizer
9
  model_name = "ArtifactAI/led_large_16384_arxiv_summarization"
10
  model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
@@ -33,17 +39,29 @@ def summarize_pdf_abstract(pdf_data):
33
  outputs = model.generate(**inputs)
34
  summary = tokenizer.decode(outputs[0], skip_special_tokens=True)
35
 
36
- speech = gTTS(summary, lang="en")
37
- speech_bytes = speech.get_wav_data()
 
 
 
38
 
39
  return {"summary": summary, "audio": speech_bytes}
40
 
41
- # Create Gradio interface
42
- interface = gr.Interface(
43
- fn=summarize_pdf_abstract,
44
- inputs=[gr.File(label="Upload PDF", mimetypes=["application/pdf"])],
45
- outputs=[gr.Text(label="One-sentence summary"), gr.Audio(label="Summary audio")],
46
- )
 
 
 
 
 
 
 
 
 
47
 
48
  # Launch the Hugging Face Space
49
  interface.launch(title="PDF Abstract Summarizer")
 
1
  import gradio as gr
 
2
  from PyPDF2 import PdfReader
3
  from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
4
  from gtts import gTTS
5
  from io import BytesIO
6
 
7
+ # Check if running in IPython environment
8
+ try:
9
+ from IPython.display import Audio
10
+ ipython_available = True
11
+ except ImportError:
12
+ ipython_available = False
13
+
14
  # Define model and tokenizer
15
  model_name = "ArtifactAI/led_large_16384_arxiv_summarization"
16
  model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
 
39
  outputs = model.generate(**inputs)
40
  summary = tokenizer.decode(outputs[0], skip_special_tokens=True)
41
 
42
+ if ipython_available:
43
+ speech = gTTS(summary, lang="en")
44
+ speech_bytes = speech.get_wav_data()
45
+ else:
46
+ speech_bytes = None
47
 
48
  return {"summary": summary, "audio": speech_bytes}
49
 
50
+ # Modify the Gradio interface based on IPython availability
51
+ if ipython_available:
52
+ # If running in IPython, include the Audio component
53
+ interface = gr.Interface(
54
+ fn=summarize_pdf_abstract,
55
+ inputs=[gr.File(label="Upload PDF", mimetypes=["application/pdf"])],
56
+ outputs=[gr.Text(label="One-sentence summary"), gr.Audio(label="Summary audio")],
57
+ )
58
+ else:
59
+ # If not running in IPython, exclude the Audio component
60
+ interface = gr.Interface(
61
+ fn=summarize_pdf_abstract,
62
+ inputs=[gr.File(label="Upload PDF", mimetypes=["application/pdf"])],
63
+ outputs=[gr.Text(label="One-sentence summary")],
64
+ )
65
 
66
  # Launch the Hugging Face Space
67
  interface.launch(title="PDF Abstract Summarizer")