File size: 3,288 Bytes
3a9cb94
 
 
 
 
7125b38
3a9cb94
7b218e5
 
3a9cb94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7b218e5
3a9cb94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7125b38
 
 
3a9cb94
 
 
 
 
 
 
 
7125b38
 
 
 
 
 
 
7b218e5
 
7125b38
7b218e5
7125b38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a9cb94
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import requests
from bs4 import BeautifulSoup
from fastapi import FastAPI
from pydantic import BaseModel
import re
import os

token = os.environ.get("huggingface_token")
key = os.environ.get("rapid_key")

app = FastAPI()


print("hello world")

@app.get("/")
async def root():
    return {"status": "OK"}

class Item(BaseModel):
    url: str
    percentage: int
    


def extract_article_content(url):
    try:
        # Fetch the HTML content of the article URL
        response = requests.get(url)

        soup = BeautifulSoup(response.text, 'html.parser')
        results = soup.find_all(['h1', 'p'])
        text = [result.text for result in results]
        ARTICLE = ' '.join(text)
        
        return ARTICLE
    except Exception as e:
        return f"Error: {e}"


@app.post("/summarize-v1")
async def root(item: Item):
    
    try:

        article = extract_article_content(item.url)

        url = "https://text-analysis12.p.rapidapi.com/summarize-text/api/v1.1"

        payload = {
            "language": "english",
            "summary_percent": item.percentage,
            'text': article
                   }
        headers = {
            "content-type": "application/json",
            "X-RapidAPI-Key": key,
            "X-RapidAPI-Host": "text-analysis12.p.rapidapi.com"
        }

        response = requests.post(url, json=payload, headers=headers).json()


        #text processing
        text = response["summary"].replace('\"', " ");
        text = re.sub(r'\s+', ' ', text)
        

        #return {clean_response}
        return {
                "summary":text}
        
    except requests.RequestException as e:
        return {"error": str(e), "status_code": 500}
    
@app.post("/summarize-v2")
async def root(item: Item):
    
    try:

        article = extract_article_content(item.url)

        response = requests.post('https://fumes-api.onrender.com/llama3',
        json={'prompt': "{ 'User': 'Summarize the following news article: '" + article + "}",
        "temperature":0.6,
        "topP":0.9,
        "maxTokens": 200}, stream=True)

        response_content = response.content.decode('utf-8')

        response_content = response_content.replace("Here is a summary of the news article:", "")
        response_content = response_content.replace("YOU CAN BUY ME COFFE! https://buymeacoffee.com/mygx", "")

        #return {clean_response}
        return {
                "summary":response_content}
        
    except requests.RequestException as e:
        return {"error": str(e), "status_code": 500}
    

@app.post("/summarize-v3")
async def root(item: Item):
    
    try:

        article = extract_article_content(item.url)

        

        API_URL = "https://api-inference.huggingface.co/models/facebook/bart-large-cnn"
        headers = {'Authorization': token}


        def query(payload):
            response = requests.post(API_URL, headers=headers, json=payload)
            return response.json()
            
        params = {'do_sample': False}

        
        output = query({
            'inputs': article,
            'parameters': params
        })
    
        return {"summary": output}
        
    except requests.RequestException as e:
        return {"error": str(e), "status_code": 500}