Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from fastapi import FastAPI, Request
|
| 3 |
+
from google.generativeai.generative_models import GenerativeModel
|
| 4 |
+
from google.generativeai import configure
|
| 5 |
+
import asyncio
|
| 6 |
+
from playwright.async_api import async_playwright
|
| 7 |
+
|
| 8 |
+
# Configure the Gemini API client with your API key
|
| 9 |
+
configure(api_key=os.environ.get("GEMINI_API_KEY"))
|
| 10 |
+
model = GenerativeModel(model_name="gemini-1.5-pro")
|
| 11 |
+
|
| 12 |
+
app = FastAPI()
|
| 13 |
+
|
| 14 |
+
async def get_page_content(url):
|
| 15 |
+
async with async_playwright() as p:
|
| 16 |
+
browser = await p.chromium.launch(headless=True)
|
| 17 |
+
page = await browser.newPage()
|
| 18 |
+
await page.goto(url)
|
| 19 |
+
content = await page.content()
|
| 20 |
+
await browser.close()
|
| 21 |
+
return content
|
| 22 |
+
|
| 23 |
+
@app.post("/process-url")
|
| 24 |
+
async def process_url(request: Request):
|
| 25 |
+
try:
|
| 26 |
+
data = await request.json()
|
| 27 |
+
url = data.get('url')
|
| 28 |
+
if not url:
|
| 29 |
+
return {"error": "URL not provided"}
|
| 30 |
+
|
| 31 |
+
# Scrape content using Playwright
|
| 32 |
+
html_content = await get_page_content(url)
|
| 33 |
+
|
| 34 |
+
# Craft the prompt for the Gemini API
|
| 35 |
+
prompt = f"Analyze the following HTML content and provide a summary of the main points:\n\n{html_content}"
|
| 36 |
+
|
| 37 |
+
# Call the Gemini API
|
| 38 |
+
response = model.generate_content(prompt)
|
| 39 |
+
summary = response.text
|
| 40 |
+
|
| 41 |
+
return {"url": url, "summary": summary}
|
| 42 |
+
|
| 43 |
+
except Exception as e:
|
| 44 |
+
return {"error": str(e)}
|