File size: 1,528 Bytes
6f14d8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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}"

@router.post("/api/scrape")
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))

@router.get("/history")
async def show_history(request: Request):
    # You can add history functionality later if needed
    return templates.TemplateResponse("history.html", {"request": request})