Download main.py from Mhammadreza1371/shadow-intelligence-api: direct link, hf CLI and curl.
- Browser
- Download file 3.04 kB
-
https://huggingface.co/spaces/Mhammadreza1371/shadow-intelligence-api/resolve/main/main.py
- Command line
-
hf download hf://spaces/Mhammadreza1371/shadow-intelligence-api/main.py
-
curl -L -o main.py https://huggingface.co/spaces/Mhammadreza1371/shadow-intelligence-api/resolve/main/main.py
3.04 kB
| from fastapi import FastAPI, HTTPException | |
| from pydantic import BaseModel | |
| import os | |
| import requests | |
| from bs4 import BeautifulSoup | |
| import google.generativeai as genai | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from typing import List | |
| import time | |
| app = FastAPI() | |
| # تنظیمات CORS برای اتصال بدون مشکل به فرانتاَند | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # پیکربندی هوش مصنوعی | |
| genai.configure(api_key=os.getenv("GEMINI_API_KEY")) | |
| class TrendRequest(BaseModel): | |
| topic: str | |
| context: str | |
| class ChannelRequest(BaseModel): | |
| channels: List[str] | |
| def get_latest_eitaa_posts(channel_id): | |
| try: | |
| url = f"https://eitaa.com/{channel_id}" | |
| # اضافه کردن هدر برای شبیهسازی مرورگر و کاهش احتمال مسدود شدن | |
| headers = { | |
| 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36' | |
| } | |
| response = requests.get(url, headers=headers, timeout=15) | |
| if response.status_code != 200: | |
| return [] | |
| soup = BeautifulSoup(response.text, 'html.parser') | |
| # پیدا کردن ظرف پیامها در ایتا وب | |
| messages = soup.find_all('div', class_='eitaa-message-text') | |
| extracted_data = [] | |
| for msg in messages[-5:]: # دریافت ۵ پیام آخر | |
| text = msg.get_text(separator="\n").strip() | |
| if text: | |
| extracted_data.append({ | |
| "text": text, | |
| "channel": channel_id, | |
| "url": url | |
| }) | |
| return extracted_data | |
| except Exception as e: | |
| print(f"Error scraping {channel_id}: {e}") | |
| return [] | |
| def home(): | |
| return {"status": "Online", "service": "Shadow Intelligence API"} | |
| def generate(request: TrendRequest): | |
| try: | |
| model = genai.GenerativeModel('gemini-1.5-flash') | |
| prompt = f"به عنوان یک متخصص رسانه، برای این موضوع در ایتا متن بنویس:\nموضوع: {request.topic}\nمحورها: {request.context}" | |
| response = model.generate_content(prompt) | |
| return {"result": response.text} | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| def scan_channels(request: ChannelRequest): | |
| all_posts = [] | |
| for channel in request.channels: | |
| posts = get_latest_eitaa_posts(channel) | |
| all_posts.extend(posts) | |
| time.sleep(1) # وقفه کوتاه برای جلوگیری از مسدود شدن آیپی | |
| # خروجی اکنون به صورت لیستی از اشیاء است که فرانتاَند متوجه میشود | |
| return { | |
| "message": "رصد موفقیتآمیز بود", | |
| "data": all_posts | |
| } |