Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import requests
|
2 |
from bs4 import BeautifulSoup
|
3 |
from fastapi import FastAPI#, Request
|
4 |
-
|
5 |
from pydantic import BaseModel
|
6 |
import re
|
7 |
import replicate
|
@@ -51,8 +51,8 @@ async def root(item: Item):
|
|
51 |
event_list.append(str(event))
|
52 |
|
53 |
# After the event stream ends, process the collected events
|
54 |
-
output_variable = "
|
55 |
-
return
|
56 |
|
57 |
except requests.RequestException as e:
|
58 |
return {"error": str(e), "status_code": 500}
|
@@ -60,31 +60,27 @@ async def root(item: Item):
|
|
60 |
|
61 |
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
# if len(article) == 0:
|
69 |
-
# return StreamingResponse(content="", media_type="application/json")
|
70 |
-
|
71 |
-
# response = requests.post('https://fumes-api.onrender.com/llama3',
|
72 |
-
# json={'prompt': "{ 'User': 'Summarize the following news article: '" + article + "}",
|
73 |
-
# "temperature":0.6,
|
74 |
-
# "topP":0.9,
|
75 |
-
# "maxTokens": 200}, stream=True)
|
76 |
-
|
77 |
-
# async def send_chunks():
|
78 |
-
# for chunk in response.iter_content(chunk_size=1024):
|
79 |
-
# if chunk:
|
80 |
-
# yield chunk.decode('utf-8')
|
81 |
|
82 |
-
|
|
|
83 |
|
84 |
-
|
85 |
-
|
|
|
|
|
|
|
|
|
|
|
86 |
|
|
|
|
|
87 |
|
|
|
|
|
88 |
|
89 |
@app.post("/extract-content")
|
90 |
async def root(item: Item):
|
|
|
1 |
import requests
|
2 |
from bs4 import BeautifulSoup
|
3 |
from fastapi import FastAPI#, Request
|
4 |
+
from fastapi.responses import StreamingResponse
|
5 |
from pydantic import BaseModel
|
6 |
import re
|
7 |
import replicate
|
|
|
51 |
event_list.append(str(event))
|
52 |
|
53 |
# After the event stream ends, process the collected events
|
54 |
+
output_variable = "".join(event_list)
|
55 |
+
return output_variable
|
56 |
|
57 |
except requests.RequestException as e:
|
58 |
return {"error": str(e), "status_code": 500}
|
|
|
60 |
|
61 |
|
62 |
|
63 |
+
@app.post("/summarize-v2")
|
64 |
+
async def root(item: Item):
|
65 |
+
try:
|
66 |
+
article = extract_article_content(item.url)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
+
if len(article) == 0:
|
69 |
+
return {'summary': ""}
|
70 |
|
71 |
+
def event_generator():
|
72 |
+
for event in replicate.stream("snowflake/snowflake-arctic-instruct", input={
|
73 |
+
"prompt": "summarize this following news article:" + article,
|
74 |
+
"temperature": 0.2
|
75 |
+
}):
|
76 |
+
# Yield the event as a string
|
77 |
+
yield str(event)
|
78 |
|
79 |
+
# Use StreamingResponse to stream the events
|
80 |
+
return StreamingResponse(event_generator())
|
81 |
|
82 |
+
except requests.RequestException as e:
|
83 |
+
return {"error": str(e), "status_code": 500}
|
84 |
|
85 |
@app.post("/extract-content")
|
86 |
async def root(item: Item):
|