Spaces:
Sleeping
Sleeping
KeksDev9513
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify, send_from_directory
|
2 |
+
import requests
|
3 |
+
import io
|
4 |
+
import random
|
5 |
+
import os
|
6 |
+
from PIL import Image
|
7 |
+
from datetime import datetime
|
8 |
+
import string
|
9 |
+
import re
|
10 |
+
|
11 |
+
app = Flask(__name__)
|
12 |
+
|
13 |
+
API_URL = "https://api-inference.huggingface.co/models/mohsenfayyaz/toxicity-classifier"
|
14 |
+
API_TOKEN = os.getenv("HF_READ_TOKEN") # it is free
|
15 |
+
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
16 |
+
|
17 |
+
def query(prompt):
|
18 |
+
payload = {
|
19 |
+
"inputs": prompt
|
20 |
+
}
|
21 |
+
|
22 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
23 |
+
response.raise_for_status()
|
24 |
+
return response.json()
|
25 |
+
|
26 |
+
@app.route("/generate", methods=["POST"])
|
27 |
+
def generate():
|
28 |
+
try:
|
29 |
+
data = request.get_json()
|
30 |
+
prompt = data["prompt"]
|
31 |
+
|
32 |
+
resp = query(prompt)
|
33 |
+
response = {
|
34 |
+
"success": True,
|
35 |
+
"result": resp
|
36 |
+
}
|
37 |
+
|
38 |
+
except Exception as e:
|
39 |
+
response = {
|
40 |
+
"success": False,
|
41 |
+
"error": str(e)
|
42 |
+
}
|
43 |
+
|
44 |
+
return jsonify(response)
|
45 |
+
|
46 |
+
if __name__ == "__main__":
|
47 |
+
app.run(debug=True, host="0.0.0.0", port=7860)
|