Update main.py
Browse files
main.py
CHANGED
|
@@ -1,12 +1,15 @@
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
from pydantic import BaseModel
|
| 3 |
import os
|
|
|
|
|
|
|
| 4 |
import google.generativeai as genai
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
-
# تنظیم CORS
|
| 10 |
app.add_middleware(
|
| 11 |
CORSMiddleware,
|
| 12 |
allow_origins=["*"],
|
|
@@ -14,30 +17,77 @@ app.add_middleware(
|
|
| 14 |
allow_headers=["*"],
|
| 15 |
)
|
| 16 |
|
| 17 |
-
# پیکربندی Gemini
|
|
|
|
| 18 |
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
| 19 |
|
|
|
|
| 20 |
class TrendRequest(BaseModel):
|
| 21 |
topic: str
|
| 22 |
context: str
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
@app.get("/")
|
| 25 |
def home():
|
| 26 |
-
return {"
|
| 27 |
|
|
|
|
| 28 |
@app.post("/generate")
|
| 29 |
def generate(request: TrendRequest):
|
| 30 |
try:
|
| 31 |
model = genai.GenerativeModel('gemini-1.5-flash')
|
| 32 |
|
| 33 |
prompt = f"""
|
| 34 |
-
|
| 35 |
موضوع: {request.topic}
|
| 36 |
-
محورها: {request.context}
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
"""
|
| 39 |
|
| 40 |
response = model.generate_content(prompt)
|
| 41 |
return {"result": response.text}
|
| 42 |
except Exception as e:
|
| 43 |
-
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
from pydantic import BaseModel
|
| 3 |
import os
|
| 4 |
+
import requests
|
| 5 |
+
from bs4 import BeautifulSoup
|
| 6 |
import google.generativeai as genai
|
| 7 |
from fastapi.middleware.cors import CORSMiddleware
|
| 8 |
+
from typing import List
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
|
| 12 |
+
# ۱. تنظیمات CORS: اجازه دسترسی به فرانتباند (React)
|
| 13 |
app.add_middleware(
|
| 14 |
CORSMiddleware,
|
| 15 |
allow_origins=["*"],
|
|
|
|
| 17 |
allow_headers=["*"],
|
| 18 |
)
|
| 19 |
|
| 20 |
+
# ۲. پیکربندی هوش مصنوعی Gemini
|
| 21 |
+
# یادتان باشد GEMINI_API_KEY را در Settings > Secrets هاگینگ فیس ست کنید
|
| 22 |
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
| 23 |
|
| 24 |
+
# ۳. مدلهای داده برای ورودیهای API
|
| 25 |
class TrendRequest(BaseModel):
|
| 26 |
topic: str
|
| 27 |
context: str
|
| 28 |
|
| 29 |
+
class ChannelRequest(BaseModel):
|
| 30 |
+
channels: List[str]
|
| 31 |
+
|
| 32 |
+
# --- بخش رصدگر ایتا (Eitaa Scraper) ---
|
| 33 |
+
def get_latest_eitaa_posts(channel_id):
|
| 34 |
+
try:
|
| 35 |
+
url = f"https://eitaa.com/{channel_id}"
|
| 36 |
+
response = requests.get(url, timeout=10)
|
| 37 |
+
if response.status_code != 200:
|
| 38 |
+
return []
|
| 39 |
+
|
| 40 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
| 41 |
+
# پیدا کردن باکسهای متن پیام در نسخه وب ایتا
|
| 42 |
+
posts = soup.find_all('div', class_='eitaa-message-text')
|
| 43 |
+
# برگرداندن متن ۵ پیام آخر
|
| 44 |
+
return [post.text.strip() for post in posts[-5:] if post.text]
|
| 45 |
+
except Exception as e:
|
| 46 |
+
print(f"Error scraping {channel_id}: {e}")
|
| 47 |
+
return []
|
| 48 |
+
|
| 49 |
+
# --- APIهای اصلی ---
|
| 50 |
+
|
| 51 |
@app.get("/")
|
| 52 |
def home():
|
| 53 |
+
return {"message": "Shadow Intelligence API is Live", "status": "Ready"}
|
| 54 |
|
| 55 |
+
# الف) API تولید محتوا با هوش مصنوعی
|
| 56 |
@app.post("/generate")
|
| 57 |
def generate(request: TrendRequest):
|
| 58 |
try:
|
| 59 |
model = genai.GenerativeModel('gemini-1.5-flash')
|
| 60 |
|
| 61 |
prompt = f"""
|
| 62 |
+
به عنوان یک تحلیلگر رسانه و متخصص عملیات روانی، برای موضوع زیر یک متن تبیینی بنویس.
|
| 63 |
موضوع: {request.topic}
|
| 64 |
+
محورهای پیشنهادی: {request.context}
|
| 65 |
+
|
| 66 |
+
ویژگی متن:
|
| 67 |
+
- زبان فارسی و لحن اقناعی
|
| 68 |
+
- مناسب برای انتشار در پیامرسان ایتا
|
| 69 |
+
- استفاده از ایموجیهای مناسب
|
| 70 |
+
- کوتاه و تاثیرگذار
|
| 71 |
"""
|
| 72 |
|
| 73 |
response = model.generate_content(prompt)
|
| 74 |
return {"result": response.text}
|
| 75 |
except Exception as e:
|
| 76 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 77 |
+
|
| 78 |
+
# ب) API رصد زنده کانالها (برای دکمه شروع رصد زنده)
|
| 79 |
+
@app.post("/scan-channels")
|
| 80 |
+
def scan_channels(request: ChannelRequest):
|
| 81 |
+
results = {}
|
| 82 |
+
for channel in request.channels:
|
| 83 |
+
posts = get_latest_eitaa_posts(channel)
|
| 84 |
+
results[channel] = posts
|
| 85 |
+
|
| 86 |
+
return {
|
| 87 |
+
"message": "رصد با موفقیت انجام شد",
|
| 88 |
+
"data": results
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
if __name__ == "__main__":
|
| 92 |
+
import uvicorn
|
| 93 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|