Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import uvicorn
|
| 2 |
from fastapi import FastAPI, Depends, HTTPException
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
|
@@ -10,39 +11,33 @@ import database
|
|
| 10 |
import schemas
|
| 11 |
import moderator
|
| 12 |
|
| 13 |
-
# Initialize Database
|
| 14 |
database.init_db()
|
| 15 |
|
|
|
|
| 16 |
app = FastAPI(title="SafeStream API")
|
|
|
|
| 17 |
|
| 18 |
-
# --- FastAPI Logic ---
|
| 19 |
def get_db():
|
| 20 |
db = database.SessionLocal()
|
| 21 |
-
try:
|
| 22 |
-
|
| 23 |
-
finally:
|
| 24 |
-
db.close()
|
| 25 |
|
| 26 |
@app.post("/comments", response_model=schemas.Comment)
|
| 27 |
def create_comment(comment: schemas.CommentCreate, db: Session = Depends(get_db)):
|
| 28 |
analysis = moderator.moderator.analyze(comment.text)
|
| 29 |
db_comment = database.Comment(
|
| 30 |
-
video_id=comment.video_id,
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
timestamp=comment.timestamp,
|
| 34 |
-
is_toxic=analysis["is_toxic"],
|
| 35 |
-
toxicity_score=analysis["score"],
|
| 36 |
-
flagged_reason=analysis["reason"]
|
| 37 |
)
|
| 38 |
db.add(db_comment)
|
| 39 |
db.commit()
|
| 40 |
db.refresh(db_comment)
|
| 41 |
return db_comment
|
| 42 |
|
| 43 |
-
#
|
| 44 |
def moderate_chat(user, text):
|
| 45 |
-
# This calls your moderation logic for the UI
|
| 46 |
analysis = moderator.moderator.analyze(text)
|
| 47 |
if analysis["is_toxic"]:
|
| 48 |
return f"🚫 BLOCKED: {analysis['reason']} (Score: {analysis['score']:.2f})", "🔴 High Toxicity"
|
|
@@ -50,23 +45,20 @@ def moderate_chat(user, text):
|
|
| 50 |
|
| 51 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 52 |
gr.Markdown("# 🛡️ SafeStream AI Moderator")
|
| 53 |
-
|
| 54 |
with gr.Row():
|
| 55 |
with gr.Column(scale=2):
|
| 56 |
gr.HTML('<iframe width="100%" height="400" src="https://www.youtube.com/embed/dQw4w9WgXcQ" frameborder="0" allowfullscreen></iframe>')
|
| 57 |
-
|
| 58 |
with gr.Column(scale=1):
|
| 59 |
gr.Markdown("### Live Chat Moderation")
|
| 60 |
user_input = gr.Textbox(label="Username", value="Guest")
|
| 61 |
msg_input = gr.Textbox(label="Type a comment...", placeholder="Try typing something mean or nice!")
|
| 62 |
status_box = gr.Label(label="Moderation Status")
|
| 63 |
output_text = gr.Textbox(label="Chat Feed", interactive=False)
|
| 64 |
-
|
| 65 |
submit_btn = gr.Button("Post Comment")
|
| 66 |
submit_btn.click(moderate_chat, inputs=[user_input, msg_input], outputs=[output_text, status_box])
|
| 67 |
|
| 68 |
-
#
|
| 69 |
-
|
| 70 |
-
|
| 71 |
if __name__ == "__main__":
|
| 72 |
-
|
|
|
|
| 1 |
+
import os
|
| 2 |
import uvicorn
|
| 3 |
from fastapi import FastAPI, Depends, HTTPException
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 11 |
import schemas
|
| 12 |
import moderator
|
| 13 |
|
| 14 |
+
# 1. Initialize Database
|
| 15 |
database.init_db()
|
| 16 |
|
| 17 |
+
# 2. Setup FastAPI (for your API endpoints)
|
| 18 |
app = FastAPI(title="SafeStream API")
|
| 19 |
+
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])
|
| 20 |
|
|
|
|
| 21 |
def get_db():
|
| 22 |
db = database.SessionLocal()
|
| 23 |
+
try: yield db
|
| 24 |
+
finally: db.close()
|
|
|
|
|
|
|
| 25 |
|
| 26 |
@app.post("/comments", response_model=schemas.Comment)
|
| 27 |
def create_comment(comment: schemas.CommentCreate, db: Session = Depends(get_db)):
|
| 28 |
analysis = moderator.moderator.analyze(comment.text)
|
| 29 |
db_comment = database.Comment(
|
| 30 |
+
video_id=comment.video_id, user=comment.user, text=comment.text,
|
| 31 |
+
timestamp=comment.timestamp, is_toxic=analysis["is_toxic"],
|
| 32 |
+
toxicity_score=analysis["score"], flagged_reason=analysis["reason"]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
)
|
| 34 |
db.add(db_comment)
|
| 35 |
db.commit()
|
| 36 |
db.refresh(db_comment)
|
| 37 |
return db_comment
|
| 38 |
|
| 39 |
+
# 3. Setup Gradio UI
|
| 40 |
def moderate_chat(user, text):
|
|
|
|
| 41 |
analysis = moderator.moderator.analyze(text)
|
| 42 |
if analysis["is_toxic"]:
|
| 43 |
return f"🚫 BLOCKED: {analysis['reason']} (Score: {analysis['score']:.2f})", "🔴 High Toxicity"
|
|
|
|
| 45 |
|
| 46 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 47 |
gr.Markdown("# 🛡️ SafeStream AI Moderator")
|
|
|
|
| 48 |
with gr.Row():
|
| 49 |
with gr.Column(scale=2):
|
| 50 |
gr.HTML('<iframe width="100%" height="400" src="https://www.youtube.com/embed/dQw4w9WgXcQ" frameborder="0" allowfullscreen></iframe>')
|
|
|
|
| 51 |
with gr.Column(scale=1):
|
| 52 |
gr.Markdown("### Live Chat Moderation")
|
| 53 |
user_input = gr.Textbox(label="Username", value="Guest")
|
| 54 |
msg_input = gr.Textbox(label="Type a comment...", placeholder="Try typing something mean or nice!")
|
| 55 |
status_box = gr.Label(label="Moderation Status")
|
| 56 |
output_text = gr.Textbox(label="Chat Feed", interactive=False)
|
|
|
|
| 57 |
submit_btn = gr.Button("Post Comment")
|
| 58 |
submit_btn.click(moderate_chat, inputs=[user_input, msg_input], outputs=[output_text, status_box])
|
| 59 |
|
| 60 |
+
# 4. LAUNCH (The Fix)
|
| 61 |
+
# Instead of mounting, we run Gradio directly on port 7860.
|
| 62 |
+
# Hugging Face will automatically find it.
|
| 63 |
if __name__ == "__main__":
|
| 64 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|