Spaces:
Running
Running
v3
Browse files- app/main.py +34 -1
app/main.py
CHANGED
|
@@ -3,6 +3,7 @@ from bs4 import BeautifulSoup
|
|
| 3 |
from fastapi import FastAPI
|
| 4 |
from pydantic import BaseModel
|
| 5 |
import re
|
|
|
|
| 6 |
|
| 7 |
|
| 8 |
app = FastAPI()
|
|
@@ -51,7 +52,7 @@ async def root(item: Item):
|
|
| 51 |
}
|
| 52 |
headers = {
|
| 53 |
"content-type": "application/json",
|
| 54 |
-
"X-RapidAPI-Key": "
|
| 55 |
"X-RapidAPI-Host": "text-analysis12.p.rapidapi.com"
|
| 56 |
}
|
| 57 |
|
|
@@ -85,6 +86,9 @@ async def root(item: Item):
|
|
| 85 |
|
| 86 |
response_content = response.content.decode('utf-8')
|
| 87 |
|
|
|
|
|
|
|
|
|
|
| 88 |
#return {clean_response}
|
| 89 |
return {
|
| 90 |
"summary":response_content}
|
|
@@ -93,4 +97,33 @@ async def root(item: Item):
|
|
| 93 |
return {"error": str(e), "status_code": 500}
|
| 94 |
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
|
|
|
| 3 |
from fastapi import FastAPI
|
| 4 |
from pydantic import BaseModel
|
| 5 |
import re
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
|
| 9 |
app = FastAPI()
|
|
|
|
| 52 |
}
|
| 53 |
headers = {
|
| 54 |
"content-type": "application/json",
|
| 55 |
+
"X-RapidAPI-Key": os.environ.get("rapid_key"),
|
| 56 |
"X-RapidAPI-Host": "text-analysis12.p.rapidapi.com"
|
| 57 |
}
|
| 58 |
|
|
|
|
| 86 |
|
| 87 |
response_content = response.content.decode('utf-8')
|
| 88 |
|
| 89 |
+
response_content = response_content.replace("Here is a summary of the news article:", "")
|
| 90 |
+
response_content = response_content.replace("YOU CAN BUY ME COFFE! https://buymeacoffee.com/mygx", "")
|
| 91 |
+
|
| 92 |
#return {clean_response}
|
| 93 |
return {
|
| 94 |
"summary":response_content}
|
|
|
|
| 97 |
return {"error": str(e), "status_code": 500}
|
| 98 |
|
| 99 |
|
| 100 |
+
@app.post("/summarize-v3")
|
| 101 |
+
async def root(item: Item):
|
| 102 |
+
|
| 103 |
+
try:
|
| 104 |
+
|
| 105 |
+
article = extract_article_content(item.url)
|
| 106 |
+
|
| 107 |
+
API_URL = "https://api-inference.huggingface.co/models/facebook/bart-large-cnn"
|
| 108 |
+
headers = {'Authorization': os.environ.get("huggingface_token")}
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
def query(payload):
|
| 112 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 113 |
+
return response.json()
|
| 114 |
+
|
| 115 |
+
params = {'do_sample': False}
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
output = query({
|
| 119 |
+
'inputs': article,
|
| 120 |
+
'parameters': params
|
| 121 |
+
})
|
| 122 |
+
|
| 123 |
+
return {"summary": output}
|
| 124 |
+
|
| 125 |
+
except requests.RequestException as e:
|
| 126 |
+
return {"error": str(e), "status_code": 500}
|
| 127 |
+
|
| 128 |
+
|
| 129 |
|