File size: 2,781 Bytes
72e6d3f
 
bad2aa2
 
72e6d3f
 
bad2aa2
 
72e6d3f
43e8e4e
 
 
72e6d3f
43e8e4e
72e6d3f
0a66a84
72e6d3f
 
bad2aa2
72e6d3f
 
 
 
 
 
 
 
 
7d8d250
72e6d3f
43e8e4e
 
 
 
 
 
4526ad3
 
 
 
 
 
 
 
 
 
 
494edda
4526ad3
 
c5b17a5
4526ad3
 
 
 
 
 
 
 
 
 
 
 
 
 
72e6d3f
 
43e8e4e
 
 
 
72e6d3f
43e8e4e
 
78e320e
43e8e4e
 
 
 
 
72e6d3f
43e8e4e
 
 
 
72e6d3f
43e8e4e
72e6d3f
43e8e4e
 
ca69c77
 
 
 
 
 
 
 
 
 
 
 
 
78e320e
ca69c77
 
 
72e6d3f
 
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
import requests
from bs4 import BeautifulSoup
from fastapi import FastAPI#, Request
#from fastapi.responses import StreamingResponse
from pydantic import BaseModel
import re



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

app = FastAPI()


def extract_article_content(url):
    try:
        
        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 ""


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


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

        article = extract_article_content(item.url)

        if len(article) == 0:
            return {'summary': ""}

        response = requests.post('https://fumes-api.onrender.com/llama3',
        json={'prompt': [{ "role":"user", "content" : '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 {"summary":response_content}
        
    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)

#         if len(article) == 0:
#             return StreamingResponse(content="", media_type="application/json")

#         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)

#         async def send_chunks():
#             for chunk in response.iter_content(chunk_size=1024):
#                 if chunk:
#                     yield chunk.decode('utf-8')

#         return StreamingResponse(send_chunks(), media_type="text/plain")

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


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

        article = extract_article_content(item.url)

        if len(article) == 0:
            return {'ERROR': "AHHHHHHHHH"}
        
        return {"content":article}
        
    except requests.RequestException as e:
        return {"error": str(e), "status_code": 500}