Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,16 +1,51 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from gradio_client import Client
|
3 |
import matplotlib.pyplot as plt
|
4 |
-
import numpy as np
|
5 |
import io
|
6 |
from PIL import Image
|
7 |
import base64
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
def moderate_text(text, safer_value):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
result = client.predict(
|
15 |
text,
|
16 |
safer_value,
|
@@ -35,7 +70,10 @@ demo = gr.Interface(
|
|
35 |
gr.Textbox(label="Enter Text:", placeholder="Type your text here...", lines=5),
|
36 |
gr.Slider(minimum=0.005, maximum=0.1, value=0.005, label="Personalize Safer Value: (larger value is less safe)")
|
37 |
],
|
38 |
-
outputs=[
|
|
|
|
|
|
|
39 |
title="Friendly Text Moderator",
|
40 |
description="Enter text to be moderated and adjust the safer value to see how it affects the moderation.",
|
41 |
theme="compact"
|
@@ -69,4 +107,4 @@ body {
|
|
69 |
demo.css = custom_css
|
70 |
|
71 |
# Launch the app
|
72 |
-
demo.launch()
|
|
|
1 |
+
import os
|
2 |
+
import requests
|
3 |
import gradio as gr
|
4 |
from gradio_client import Client
|
5 |
import matplotlib.pyplot as plt
|
|
|
6 |
import io
|
7 |
from PIL import Image
|
8 |
import base64
|
9 |
|
10 |
+
# Load Hugging Face token from environment variable
|
11 |
+
HF_TOKEN = os.getenv("HF_TOKEN", "your_default_hf_token")
|
12 |
|
13 |
+
def get_dynamic_endpoint():
|
14 |
+
"""
|
15 |
+
Fetch the dynamic endpoint using the Hugging Face API.
|
16 |
+
|
17 |
+
Returns:
|
18 |
+
str: The current dynamic endpoint.
|
19 |
+
"""
|
20 |
+
api_url = "https://api.huggingface.co/space/duchaba/friendly-text-moderation"
|
21 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
22 |
+
|
23 |
+
response = requests.get(api_url, headers=headers)
|
24 |
+
response.raise_for_status() # Raise an error for bad status codes
|
25 |
+
|
26 |
+
# Extract the endpoint from the response
|
27 |
+
data = response.json()
|
28 |
+
endpoint = data.get("url")
|
29 |
+
return endpoint
|
30 |
+
|
31 |
+
# Fetch the dynamic endpoint
|
32 |
+
dynamic_endpoint = get_dynamic_endpoint()
|
33 |
+
|
34 |
+
# Initialize the client with the dynamic endpoint
|
35 |
+
client = Client(dynamic_endpoint, hf_token=HF_TOKEN)
|
36 |
|
37 |
def moderate_text(text, safer_value):
|
38 |
+
"""
|
39 |
+
Moderates the given text using the Hugging Face API and returns the result and moderation pie chart.
|
40 |
+
|
41 |
+
Args:
|
42 |
+
text (str): The text to be moderated.
|
43 |
+
safer_value (float): The safer value for text moderation.
|
44 |
+
|
45 |
+
Returns:
|
46 |
+
result (dict): The moderation result.
|
47 |
+
img (PIL.Image): The moderation pie chart.
|
48 |
+
"""
|
49 |
result = client.predict(
|
50 |
text,
|
51 |
safer_value,
|
|
|
70 |
gr.Textbox(label="Enter Text:", placeholder="Type your text here...", lines=5),
|
71 |
gr.Slider(minimum=0.005, maximum=0.1, value=0.005, label="Personalize Safer Value: (larger value is less safe)")
|
72 |
],
|
73 |
+
outputs=[
|
74 |
+
gr.Textbox(label="Moderated Text:", lines=5),
|
75 |
+
gr.Image(type="pil", label="Moderation Pie Chart")
|
76 |
+
],
|
77 |
title="Friendly Text Moderator",
|
78 |
description="Enter text to be moderated and adjust the safer value to see how it affects the moderation.",
|
79 |
theme="compact"
|
|
|
107 |
demo.css = custom_css
|
108 |
|
109 |
# Launch the app
|
110 |
+
demo.launch()
|