Spaces:
Running
Running
File size: 3,886 Bytes
72e6d3f bad2aa2 f59d018 72e6d3f 7e60c15 5d8f070 f8ee914 bad2aa2 72e6d3f 43e8e4e 09c5387 72e6d3f 43e8e4e 72e6d3f 0a66a84 72e6d3f bad2aa2 72e6d3f 7d8d250 72e6d3f 43e8e4e 085c288 4526ad3 085c288 e5868d9 051a524 e5868d9 085c288 e5868d9 4526ad3 085c288 f59d018 4526ad3 72e6d3f f59d018 72e6d3f f59d018 72e6d3f f59d018 2e3bef7 09c5387 2e3bef7 f59d018 7385806 ca69c77 f59d018 7385806 ca69c77 f59d018 7385806 ca69c77 78e320e ca69c77 f8ee914 97b207f f8ee914 2bdc593 9528425 f8ee914 2bdc593 f8ee914 2bdc593 f8ee914 2bdc593 f8ee914 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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
import requests
from bs4 import BeautifulSoup
from fastapi import FastAPI#, Request
from fastapi.responses import StreamingResponse
from pydantic import BaseModel
import re
import replicate
import os
from supabase import create_client, Client
url: str = os.environ.get("DB_URL")
key: str = os.environ.get("DB_KEY")
supabase: Client = create_client(url, key)
class Item(BaseModel):
url: str
max_tokens: 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': ""}
event_list = []
for event in replicate.stream("snowflake/snowflake-arctic-instruct", input= {
"prompt": "summarize this following news article:" + article,
"temperature": 0.2}):
# Convert the event to a string and append it to the list
event_list.append(str(event))
# After the event stream ends, process the collected events
output_variable = "".join(event_list)
return output_variable
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 {'summary': ""}
def event_generator():
for event in replicate.stream("snowflake/snowflake-arctic-instruct", input={
"prompt": f"summarize this news article in {item.max_tokens} lines:" + article,
"temperature": 0.2,
"max_new_tokens" : 1000
}):
# Yield the event as a string
yield str(event)
print(str(event), end="")
# Use StreamingResponse to stream the events
return StreamingResponse(event_generator(), media_type='text/event-stream')
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}
async def insert_image(url , id):
data, count = await supabase.table('news').update({'image_url': url}).eq('id', id).execute()
@app.get("/extract-images")
async def root(site: str = 'abcnews.go'):
try:
ii_list = []
response = supabase.table('news').select("*").eq('source', f'www.{site}.com').is_('image_url', 'null').order('published_date', desc=True).limit(15).execute()
for i in range(len(response.data)):
url = response.data[i]['article_url']
try:
res = requests.get(url)
soup = BeautifulSoup(res.text, 'html.parser')
results = soup.find_all(['img'])
url = results[1]['src']
ii_list.append(url)
await insert_image(url, response.data[i]['id'] )
except Exception as e:
print(e)
results = None
return {"RESULTS": ii_list}
except requests.RequestException as e:
return {"error": str(e), "status_code": 500}
|