Spaces:
Running
Running
| import requests | |
| from bs4 import BeautifulSoup | |
| from fastapi import FastAPI, Request | |
| from fastapi.responses import StreamingResponse | |
| from pydantic import BaseModel | |
| import re | |
| import os | |
| # import transformers | |
| # import torch | |
| class Item(BaseModel): | |
| url: str | |
| percentage: int | |
| app = FastAPI() | |
| #summarizer = transformers.pipeline("summarization", model="Falconsai/text_summarization") | |
| def extract_article_content(url): | |
| try: | |
| # Fetch the HTML content of the article URL | |
| 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 "" | |
| async def root(): | |
| return {"status": "OK"} | |
| 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': "{ 'User': '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} | |
| 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} | |
| # @app.post("/summarize-v3") | |
| # async def root(item: Item): | |
| # try: | |
| # article = extract_article_content(item.url) | |
| # if len(article) == 0: | |
| # return {'summary': ""} | |
| # summ = summarizer(article, max_length=1000, min_length=30, do_sample=False) | |
| # return {"summary":summ} | |
| # except requests.RequestException as e: | |
| # return {"error": str(e), "status_code": 500} | |