Spaces:
Sleeping
Sleeping
from fastapi import APIRouter, Request, HTTPException | |
from fastapi.templating import Jinja2Templates | |
from pydantic import BaseModel, HttpUrl | |
from app.services.scraper_service import ScraperService | |
from app.services.flan_t5_service import FlanT5Service | |
from typing import Optional | |
router = APIRouter() | |
templates = Jinja2Templates(directory="app/templates") | |
scraper_service = ScraperService() | |
flan_t5_service = FlanT5Service() | |
class ScrapeRequest(BaseModel): | |
url: HttpUrl | |
prompt_template: Optional[str] = "Summarize the following text: {text}" | |
async def scrape_url(data: ScrapeRequest): | |
try: | |
# Scrape and process the URL | |
text, chunks = await scraper_service.scrape_and_process(str(data.url)) | |
# Process each chunk with Flan-T5 | |
results = [] | |
for chunk in chunks: | |
prompt = data.prompt_template.format(text=chunk.page_content) | |
response = await flan_t5_service.generate_response(prompt) | |
results.append(response) | |
# Combine results | |
final_result = " ".join(results) | |
return { | |
"success": True, | |
"result": final_result, | |
"url": str(data.url) | |
} | |
except Exception as e: | |
raise HTTPException(status_code=500, detail=str(e)) | |
async def show_history(request: Request): | |
# You can add history functionality later if needed | |
return templates.TemplateResponse("history.html", {"request": request}) |