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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -51
app.py CHANGED
@@ -1,61 +1,21 @@
1
  import os
2
- from dotenv import load_dotenv
3
- import requests
4
- import json
 
5
  import gradio as gr
6
- import logging
7
-
8
- # Set up logging
9
- logging.basicConfig(level=logging.DEBUG)
10
- logger = logging.getLogger(__name__)
11
-
12
- # Load environment variables
13
- 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 = "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()
39
- return response.json()
40
- except requests.exceptions.RequestException as e:
41
- logger.error(f"Request failed: {e}")
42
- if hasattr(e, 'response') and e.response is not None:
43
- logger.error(f"Error response content: {e.response.text}")
44
- return {"error": f"Request failed: {str(e)}"}
45
 
46
- def summarize(input_text):
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']}"
54
- else:
55
- return f"Unexpected response format: {output}"
56
- except Exception as e:
57
- logger.exception("An error occurred during summarization")
58
- return f"An error occurred: {str(e)}"
59
 
60
  demo = gr.Interface(fn=summarize, inputs="text", outputs="text")
61
  demo.launch()
 
1
  import os
2
+ import io
3
+ from dotenv import load_dotenv, find_dotenv
4
+ import requests, json
5
+ from transformers import pipeline
6
  import gradio as gr
7
+ from transformers import pipeline
 
 
 
 
 
 
 
8
 
9
  # Access the Hugging Face API key and endpoint URL
10
  hf_api_key = os.getenv('HF_API_KEY')
11
+ ENDPOINT_URL = os.getenv('HF_API_SUMMARY_BASE')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
+ #Summarization endpoint
14
+ get_completion = pipeline("summarization", model="shleifer/distilbart-cnn-12-6")
 
 
 
 
 
 
 
 
 
 
15
 
16
+ def summarize(input):
17
+ output = get_completion(input)
18
+ return output[0]['summary_text']
 
 
 
 
 
 
 
 
 
 
19
 
20
  demo = gr.Interface(fn=summarize, inputs="text", outputs="text")
21
  demo.launch()