ImHadis commited on
Commit
38b9bca
·
verified ·
1 Parent(s): 165652d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -10
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import os
2
  from dotenv import load_dotenv
3
- from transformers import pipeline
4
  import gradio as gr
5
 
6
  # Load environment variables
@@ -9,18 +9,27 @@ load_dotenv()
9
  # Access the Hugging Face API key
10
  hf_api_key = os.getenv('HF_API_KEY')
11
 
12
- # Initialize the summarization pipeline
13
- summarizer = pipeline(
14
- "summarization",
15
- model="sshleifer/distilbart-cnn-12-6", # Corrected model name
16
- token=hf_api_key
17
- )
 
18
 
19
  def summarize(input_text):
20
  try:
21
- # Limit input text to 1024 tokens to avoid potential issues
22
- output = summarizer(input_text, max_length=130, min_length=30, do_sample=False)
23
- return output[0]['summary_text']
 
 
 
 
 
 
 
 
24
  except Exception as e:
25
  return f"An error occurred: {str(e)}"
26
 
 
1
  import os
2
  from dotenv import load_dotenv
3
+ import requests
4
  import gradio as gr
5
 
6
  # Load environment variables
 
9
  # Access the Hugging Face API key
10
  hf_api_key = os.getenv('HF_API_KEY')
11
 
12
+ # Set up the API endpoint
13
+ API_URL = "https://api-inference.huggingface.co/models/sshleifer/distilbart-cnn-12-6"
14
+ headers = {"Authorization": f"Bearer {hf_api_key}"}
15
+
16
+ def query(payload):
17
+ response = requests.post(API_URL, headers=headers, json=payload)
18
+ return response.json()
19
 
20
  def summarize(input_text):
21
  try:
22
+ output = query({
23
+ "inputs": input_text,
24
+ "parameters": {"max_length": 130, "min_length": 30}
25
+ })
26
+
27
+ if isinstance(output, list) and len(output) > 0 and 'summary_text' in output[0]:
28
+ return output[0]['summary_text']
29
+ elif isinstance(output, dict) and 'error' in output:
30
+ return f"API Error: {output['error']}"
31
+ else:
32
+ return f"Unexpected response format: {output}"
33
  except Exception as e:
34
  return f"An error occurred: {str(e)}"
35