ImHadis commited on
Commit
6809479
·
verified ·
1 Parent(s): e713972

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -11
app.py CHANGED
@@ -14,29 +14,25 @@ load_dotenv()
14
 
15
  # Access the Hugging Face API key and endpoint URL
16
  hf_api_key = os.getenv('HF_API_KEY')
17
- MODEL_NAME = "sshleifer/distilbart-cnn-12-6"
18
  ENDPOINT_URL = f"https://api-inference.huggingface.co/models/{MODEL_NAME}"
19
 
20
- def get_completion(inputs, parameters=None):
21
  headers = {
22
  "Authorization": f"Bearer {hf_api_key}",
23
  "Content-Type": "application/json"
24
  }
25
- data = {
26
  "inputs": inputs,
27
- "options": {"wait_for_model": True}
28
  }
29
- if parameters is not None:
30
- data.update({"parameters": parameters})
31
 
32
  logger.debug(f"Sending request to {ENDPOINT_URL}")
33
- logger.debug(f"Request headers: {headers}")
34
- logger.debug(f"Request data: {json.dumps(data, indent=2)}")
35
 
36
  try:
37
- response = requests.post(ENDPOINT_URL, headers=headers, json=data)
38
  logger.debug(f"Response status code: {response.status_code}")
39
- logger.debug(f"Response headers: {dict(response.headers)}")
40
  logger.debug(f"Response content: {response.text}")
41
 
42
  response.raise_for_status()
@@ -51,7 +47,7 @@ def summarize(input_text):
51
  try:
52
  logger.info(f"Received input text: {input_text[:100]}...") # Log first 100 chars of input
53
  output = get_completion(input_text)
54
- if isinstance(output, list) and len(output) > 0 and 'summary_text' in output[0]:
55
  return output[0]['summary_text']
56
  elif isinstance(output, dict) and 'error' in output:
57
  return f"API Error: {output['error']}"
 
14
 
15
  # Access the Hugging Face API key and endpoint URL
16
  hf_api_key = os.getenv('HF_API_KEY')
17
+ MODEL_NAME = "facebook/bart-large-cnn"
18
  ENDPOINT_URL = f"https://api-inference.huggingface.co/models/{MODEL_NAME}"
19
 
20
+ def get_completion(inputs):
21
  headers = {
22
  "Authorization": f"Bearer {hf_api_key}",
23
  "Content-Type": "application/json"
24
  }
25
+ payload = {
26
  "inputs": inputs,
27
+ "parameters": {"max_length": 130, "min_length": 30}
28
  }
 
 
29
 
30
  logger.debug(f"Sending request to {ENDPOINT_URL}")
31
+ logger.debug(f"Request payload: {json.dumps(payload, indent=2)}")
 
32
 
33
  try:
34
+ response = requests.post(ENDPOINT_URL, headers=headers, json=payload)
35
  logger.debug(f"Response status code: {response.status_code}")
 
36
  logger.debug(f"Response content: {response.text}")
37
 
38
  response.raise_for_status()
 
47
  try:
48
  logger.info(f"Received input text: {input_text[:100]}...") # Log first 100 chars of input
49
  output = get_completion(input_text)
50
+ if isinstance(output, list) and len(output) > 0 and isinstance(output[0], dict) and 'summary_text' in output[0]:
51
  return output[0]['summary_text']
52
  elif isinstance(output, dict) and 'error' in output:
53
  return f"API Error: {output['error']}"