uumerrr684 commited on
Commit
4f5f461
·
verified ·
1 Parent(s): 267bb3a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -84
app.py CHANGED
@@ -5,92 +5,50 @@ from dotenv import load_dotenv
5
 
6
  load_dotenv()
7
 
8
- # 1. Modern Summarization Function
9
  def summarize(text):
10
- headers = {"Authorization": f"Bearer {os.getenv('HF_API_TOKEN')}"}
11
- response = requests.post(
12
- "https://api-inference.huggingface.co/models/facebook/bart-large-cnn",
13
- headers=headers,
14
- json={"inputs": text, "parameters": {"max_length": 50}}
15
- )
16
- return response.json()[0]["summary_text"]
17
-
18
- # 2. Sexy UI Configuration
19
- with gr.Blocks(
20
- theme=gr.themes.Soft(
21
- primary_hue="violet",
22
- secondary_hue="emerald",
23
- font=[gr.themes.GoogleFont("Poppins"), "sans-serif"]
24
- ),
25
- css="""
26
- .gradio-container {
27
- max-width: 800px;
28
- margin: 0 auto;
29
- border-radius: 20px !important;
30
- box-shadow: 0 10px 30px rgba(0,0,0,0.1) !important;
31
- }
32
- textarea {
33
- min-height: 200px !important;
34
- border-radius: 12px !important;
35
- }
36
- button {
37
- background: linear-gradient(135deg, #6e8efb 0%, #a777e3 100%) !important;
38
- border: none !important;
39
- }
40
- .example-image {
41
- border-radius: 12px;
42
- overflow: hidden;
43
- }
44
- """
45
- ) as app:
46
-
47
- # 3. Premium Header
48
- gr.Markdown("""
49
- <div style="text-align: center;">
50
- <h1 style="font-weight: 800;">✨ Summify Pro</h1>
51
- <p style="color: #666;">AI-powered text condensation at your fingertips</p>
52
- </div>
53
- """)
54
-
55
- # 4. Dual-Panel Layout
56
- with gr.Row(equal_height=True):
57
- with gr.Column():
58
- input_txt = gr.Textbox(
59
- label="Your Content",
60
- placeholder="Paste articles, essays, or documents here...",
61
- lines=10,
62
- elem_classes=["fancy-input"]
63
- )
64
- gr.Examples(
65
- examples=[
66
- "The quick brown fox jumps over the lazy dog...",
67
- "He prayed Fajr half-asleep, unaware of the divine protection..."
68
- ],
69
- inputs=input_txt,
70
- label="Try These:"
71
- )
72
-
73
- with gr.Column():
74
- output_txt = gr.Textbox(
75
- label="AI Summary",
76
- interactive=False,
77
- lines=10,
78
- elem_classes=["fancy-output"]
79
- )
80
- with gr.Row():
81
- submit_btn = gr.Button("Generate Summary", variant="primary")
82
- clear_btn = gr.Button("Clear")
83
-
84
- # 5. Interactive Functions
85
- submit_btn.click(
86
  fn=summarize,
87
  inputs=input_txt,
88
- outputs=output_txt
89
- )
90
- clear_btn.click(
91
- fn=lambda: ("", ""),
92
- inputs=None,
93
- outputs=[input_txt, output_txt]
94
  )
95
 
96
- app.launch()
 
5
 
6
  load_dotenv()
7
 
 
8
  def summarize(text):
9
+ """Foolproof summarization with full error handling"""
10
+ try:
11
+ # 1. Verify token
12
+ token = os.getenv("HF_API_TOKEN")
13
+ if not token:
14
+ return "⚠️ Token missing: Set HF_API_TOKEN in Secrets", ""
15
+
16
+ # 2. Call API with timeout
17
+ response = requests.post(
18
+ "https://api-inference.huggingface.co/models/facebook/bart-large-cnn",
19
+ headers={"Authorization": f"Bearer {token}"},
20
+ json={"inputs": text, "parameters": {"max_length": 50}},
21
+ timeout=30
22
+ )
23
+
24
+ # 3. Handle all response cases
25
+ if response.status_code == 200:
26
+ return response.json()[0].get("summary_text", "⚠️ Empty response"), "✅ Success"
27
+ elif response.status_code == 503:
28
+ return "⚠️ Model is loading... Try again in 30s", "🔄 Loading"
29
+ else:
30
+ return f"⚠️ API Error ({response.status_code})", "❌ Failed"
31
+
32
+ except requests.exceptions.RequestException as e:
33
+ return f"⚠️ Network error: {str(e)}", "❌ Network Issue"
34
+ except Exception as e:
35
+ return f"⚠️ Unexpected error: {str(e)}", "❌ Error"
36
+
37
+ # Gradio UI with error display
38
+ with gr.Blocks(theme=gr.themes.Soft()) as app:
39
+ gr.Markdown("# ✨ Summify (Stable Version)")
40
+
41
+ with gr.Row():
42
+ input_txt = gr.Textbox(label="Input Text", lines=7, placeholder="Paste any text...")
43
+ output_txt = gr.Textbox(label="Summary", lines=7, interactive=False)
44
+
45
+ status = gr.Textbox(label="Status", interactive=False)
46
+ btn = gr.Button("Summarize", variant="primary")
47
+
48
+ btn.click(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  fn=summarize,
50
  inputs=input_txt,
51
+ outputs=[output_txt, status]
 
 
 
 
 
52
  )
53
 
54
+ app.launch()