anky2002 commited on
Commit
c0e6e9e
·
1 Parent(s): 9f9be34
Files changed (1) hide show
  1. app.py +29 -10
app.py CHANGED
@@ -1,8 +1,14 @@
1
  import gradio as gr
2
  from transformers import pipeline
 
3
 
4
- # Initialize the model
5
- model = pipeline("summarization", model="luisotorres/bart-finetuned-samsum")
 
 
 
 
 
6
 
7
  def summarize_text(text):
8
  try:
@@ -10,22 +16,35 @@ def summarize_text(text):
10
  input_length = len(text.split())
11
  max_length = min(130, max(30, input_length // 2))
12
 
13
- summary = model(text, max_length=max_length, min_length=30)
 
 
 
14
  return summary[0]["summary_text"]
15
  except Exception as e:
16
- return str(e)
17
 
18
  # Create Gradio interface
19
  iface = gr.Interface(
20
  fn=summarize_text,
21
- inputs=gr.Textbox(label="Input Text", lines=5),
 
 
 
 
22
  outputs=gr.Textbox(label="Summary"),
23
  title="Text Summarization",
24
- description="Enter your text to generate a summary.",
25
  examples=[
26
- ["Sarah: Do you think it's a good idea to invest in Bitcoin?\nEmily: I'm skeptical. The market is very volatile, and you could lose money.\nSarah: True. But there's also a high upside, right?"]
27
- ]
 
 
28
  )
29
 
30
- # Launch the interface with public URL enabled
31
- iface.launch(share=True)
 
 
 
 
 
1
  import gradio as gr
2
  from transformers import pipeline
3
+ import torch
4
 
5
+ # Check if CUDA is available and set device
6
+ device = "cuda" if torch.cuda.is_available() else "cpu"
7
+
8
+ # Initialize the model with device setting
9
+ model = pipeline("summarization",
10
+ model="luisotorres/bart-finetuned-samsum",
11
+ device=device)
12
 
13
  def summarize_text(text):
14
  try:
 
16
  input_length = len(text.split())
17
  max_length = min(130, max(30, input_length // 2))
18
 
19
+ summary = model(text,
20
+ max_length=max_length,
21
+ min_length=30,
22
+ do_sample=False) # Deterministic generation
23
  return summary[0]["summary_text"]
24
  except Exception as e:
25
+ return f"Error: {str(e)}"
26
 
27
  # Create Gradio interface
28
  iface = gr.Interface(
29
  fn=summarize_text,
30
+ inputs=gr.Textbox(
31
+ label="Input Text",
32
+ lines=5,
33
+ placeholder="Enter the text you want to summarize..."
34
+ ),
35
  outputs=gr.Textbox(label="Summary"),
36
  title="Text Summarization",
37
+ description="Enter your text to generate a concise summary. The summary length will automatically adjust based on your input length.",
38
  examples=[
39
+ ["Sarah: Do you think it's a good idea to invest in Bitcoin?\nEmily: I'm skeptical. The market is very volatile, and you could lose money.\nSarah: True. But there's also a high upside, right?"],
40
+ ["John: Hey, can you help me with the project?\nMary: Sure, what do you need?\nJohn: I'm stuck on the database design.\nMary: OK, let's schedule a call tomorrow morning.\nJohn: Perfect, thanks!"]
41
+ ],
42
+ allow_flagging="never"
43
  )
44
 
45
+ # Launch the interface without share parameter
46
+ iface.launch(
47
+ server_name="0.0.0.0", # Required for Spaces
48
+ server_port=7860, # Standard port for Spaces
49
+ show_error=True
50
+ )