Techdude01 commited on
Commit
491b473
·
verified ·
1 Parent(s): 889af62

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -0
app.py CHANGED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import gradio as gr
3
+ from accelerate import Accelerator
4
+ from transformers import pipeline
5
+
6
+ # Initialize the accelerator
7
+ accelerator = Accelerator()
8
+
9
+ # Print a description of the current configuration
10
+ print("Accelerator State:", accelerator.state)
11
+
12
+ # Define the path to your custom model
13
+ model_path = (
14
+ "../Models/models--sshleifer--distilbart-cnn-12-6/snapshots"
15
+ "/a4f8f3ea906ed274767e9906dbaede7531d660ff"
16
+ )
17
+
18
+ # Initialize the text summarization pipeline
19
+ try:
20
+ text_summary = pipeline(
21
+ "summarization",
22
+ model=model_path,
23
+ torch_dtype=torch.bfloat16, # Use bfloat16 for better performance on supported hardware
24
+ device=0 if torch.cuda.is_available() else -1 # Use GPU if available
25
+ )
26
+ except Exception as e:
27
+ print(f"Error initializing the summarization pipeline: {e}")
28
+ raise
29
+
30
+ # Define the Gradio interface function
31
+ def summary(input_text):
32
+ try:
33
+ output = text_summary(input_text)
34
+ return output[0]['summary_text']
35
+ except Exception as e:
36
+ return f"An error occurred while summarizing: {e}"
37
+
38
+ # Close any existing Gradio interfaces
39
+ gr.close_all()
40
+
41
+ # Define the Gradio interface
42
+ demo = gr.Interface(
43
+ fn=summary,
44
+ inputs=[gr.Textbox(label="Input text to summarize", lines=6)],
45
+ outputs=[gr.Textbox(label="Summarized text", lines=4)],
46
+ title="GenAIProject01: Text Summarizer",
47
+ description="THIS APPLICATION SUMMARIZE INPUT TEXT USING A PRE-TRAINED MODEL."
48
+ )
49
+
50
+ # Launch the Gradio app
51
+ demo.launch()