Spaces:
Running
Running
Delete app
Browse files- app/main.py +0 -128
app/main.py
DELETED
@@ -1,128 +0,0 @@
|
|
1 |
-
import requests
|
2 |
-
from bs4 import BeautifulSoup
|
3 |
-
from fastapi import FastAPI
|
4 |
-
from pydantic import BaseModel
|
5 |
-
import re
|
6 |
-
import os
|
7 |
-
|
8 |
-
token = os.environ.get("huggingface_token")
|
9 |
-
|
10 |
-
app = FastAPI()
|
11 |
-
|
12 |
-
|
13 |
-
print("hello world")
|
14 |
-
|
15 |
-
@app.get("/")
|
16 |
-
async def root():
|
17 |
-
return {"status": "OK"}
|
18 |
-
|
19 |
-
class Item(BaseModel):
|
20 |
-
url: str
|
21 |
-
percentage: int
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
def extract_article_content(url):
|
26 |
-
try:
|
27 |
-
# Fetch the HTML content of the article URL
|
28 |
-
response = requests.get(url)
|
29 |
-
|
30 |
-
soup = BeautifulSoup(response.text, 'html.parser')
|
31 |
-
results = soup.find_all(['h1', 'p'])
|
32 |
-
text = [result.text for result in results]
|
33 |
-
ARTICLE = ' '.join(text)
|
34 |
-
|
35 |
-
return ARTICLE
|
36 |
-
except Exception as e:
|
37 |
-
return f"Error: {e}"
|
38 |
-
|
39 |
-
|
40 |
-
@app.post("/summarize-v1")
|
41 |
-
async def root(item: Item):
|
42 |
-
|
43 |
-
try:
|
44 |
-
|
45 |
-
article = extract_article_content(item.url)
|
46 |
-
|
47 |
-
url = "https://text-analysis12.p.rapidapi.com/summarize-text/api/v1.1"
|
48 |
-
|
49 |
-
payload = {
|
50 |
-
"language": "english",
|
51 |
-
"summary_percent": item.percentage,
|
52 |
-
'text': article
|
53 |
-
}
|
54 |
-
headers = {
|
55 |
-
"content-type": "application/json",
|
56 |
-
"X-RapidAPI-Key":"",
|
57 |
-
"X-RapidAPI-Host": "text-analysis12.p.rapidapi.com"
|
58 |
-
}
|
59 |
-
|
60 |
-
response = requests.post(url, json=payload, headers=headers).json()
|
61 |
-
|
62 |
-
|
63 |
-
#text processing
|
64 |
-
text = response["summary"].replace('\"', " ");
|
65 |
-
text = re.sub(r'\s+', ' ', text)
|
66 |
-
|
67 |
-
|
68 |
-
#return {clean_response}
|
69 |
-
return {
|
70 |
-
"summary":text}
|
71 |
-
|
72 |
-
except requests.RequestException as e:
|
73 |
-
return {"error": str(e), "status_code": 500}
|
74 |
-
|
75 |
-
@app.post("/summarize-v2")
|
76 |
-
async def root(item: Item):
|
77 |
-
|
78 |
-
try:
|
79 |
-
|
80 |
-
article = extract_article_content(item.url)
|
81 |
-
|
82 |
-
response = requests.post('https://fumes-api.onrender.com/llama3',
|
83 |
-
json={'prompt': "{ 'User': 'Summarize the following news article: '" + article + "}",
|
84 |
-
"temperature":0.6,
|
85 |
-
"topP":0.9,
|
86 |
-
"maxTokens": 200}, stream=True)
|
87 |
-
|
88 |
-
response_content = response.content.decode('utf-8')
|
89 |
-
|
90 |
-
response_content = response_content.replace("Here is a summary of the news article:", "")
|
91 |
-
response_content = response_content.replace("YOU CAN BUY ME COFFE! https://buymeacoffee.com/mygx", "")
|
92 |
-
|
93 |
-
#return {clean_response}
|
94 |
-
return {
|
95 |
-
"summary":response_content}
|
96 |
-
|
97 |
-
except requests.RequestException as e:
|
98 |
-
return {"error": str(e), "status_code": 500}
|
99 |
-
|
100 |
-
|
101 |
-
@app.post("/summarize-v3")
|
102 |
-
async def root(item: Item):
|
103 |
-
|
104 |
-
try:
|
105 |
-
|
106 |
-
article = extract_article_content(item.url)
|
107 |
-
|
108 |
-
|
109 |
-
API_URL = "https://api-inference.huggingface.co/models/facebook/bart-large-cnn"
|
110 |
-
headers = {'Authorization': token}
|
111 |
-
|
112 |
-
|
113 |
-
params = {'do_sample': False}
|
114 |
-
|
115 |
-
response = requests.post(API_URL, headers=headers, json={
|
116 |
-
'inputs': article,
|
117 |
-
'parameters': params
|
118 |
-
})
|
119 |
-
|
120 |
-
output = response.json()
|
121 |
-
|
122 |
-
return {"summary": output}
|
123 |
-
|
124 |
-
except requests.RequestException as e:
|
125 |
-
return {"error": str(e), "status_code": 500}
|
126 |
-
|
127 |
-
|
128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|