sivan26 commited on
Commit
db24609
·
verified ·
1 Parent(s): 1bc4221

create app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -0
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ import gradio as gr
3
+ from transformers import pipeline
4
+
5
+ # --- 1. Load the Text Summarization Model ---
6
+ # We're using a pre-trained summarization model from Hugging Face.
7
+ # 'sshleifer/distilbart-cnn-12-6' is a good balance of speed and quality.
8
+ # The 'pipeline' function simplifies using these models.
9
+ print("Loading summarization model... This may take a moment.")
10
+ try:
11
+ summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
12
+ print("Model loaded successfully!")
13
+ except Exception as e:
14
+ print(f"Error loading model: {e}")
15
+ # Fallback or exit if the model can't be loaded, or handle gracefully.
16
+ # For this demo, we'll let it fail for simplicity in error handling.
17
+ summarizer = None # Ensure summarizer is None if loading fails
18
+
19
+ # --- 2. Define the Summarization Function ---
20
+ # This function will be called when the user clicks the 'Summarize' button.
21
+ def summarize_text(input_text):
22
+ """
23
+ Takes an input string and returns a concise summary.
24
+ """
25
+ if not input_text.strip(): # Check if the input text is empty or just whitespace
26
+ return "Please enter some text to summarize."
27
+
28
+ if summarizer is None:
29
+ return "Summarization model failed to load. Please try again later or check server logs."
30
+
31
+ try:
32
+ # The summarizer pipeline returns a list of dictionaries.
33
+ # We're interested in the 'summary_text' key from the first item.
34
+ summary = summarizer(input_text, max_length=150, min_length=30, do_sample=False)
35
+ return summary[0]['summary_text']
36
+ except Exception as e:
37
+ # Basic error handling for summarization issues
38
+ return f"An error occurred during summarization: {e}"
39
+
40
+ # --- 3. Create the Gradio Interface ---
41
+ # Gradio makes it easy to build a web UI for machine learning models.
42
+ # `fn`: The function to call when the interface is used.
43
+ # `inputs`: The type of input component (here, a Textbox).
44
+ # `outputs`: The type of output component (here, a Textbox).
45
+ # `title` and `description` are used for the app's heading and explanation.
46
+ iface = gr.Interface(
47
+ fn=summarize_text,
48
+ inputs=gr.Textbox(lines=10, placeholder="Paste your text here to get a summary...", label="Input Text"),
49
+ outputs=gr.Textbox(lines=7, label="Summary"),
50
+ title="Simple AI Text Summarizer",
51
+ description=(
52
+ "This demo application uses a Hugging Face pre-trained model (DistilBART) "
53
+ "to generate a concise summary of your input text. "
54
+ "Simply type or paste your text into the box below and click 'Submit'."
55
+ ),
56
+ live=False # Set to True if you want real-time summarization as you type
57
+ )
58
+
59
+ # --- 4. Launch the Gradio App ---
60
+ # This line starts the web server for the Gradio app.
61
+ # share=True generates a public link (useful for sharing demos temporarily).
62
+ # debug=True provides more detailed logging in the console.
63
+ if __name__ == "__main__":
64
+ iface.launch(share=False, debug=False)
65
+