Srini Vangala commited on
Commit
4dd6ed2
1 Parent(s): 24d7353

Removed the dynamic url

Browse files
Files changed (2) hide show
  1. app.py +30 -2
  2. fetchURL.txt +111 -0
app.py CHANGED
@@ -5,12 +5,40 @@ import numpy as np
5
  import io
6
  from PIL import Image
7
  import base64
8
-
 
9
  #client = Client("https://duchaba-friendly-text-moderation.hf.space/--replicas/gffry/")
10
- client = Client("https://duchaba-friendly-text-moderation.hf.space/--replicas/6rx2j/")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
 
13
  def moderate_text(text, safer_value):
 
 
 
 
 
14
  result = client.predict(
15
  text,
16
  safer_value,
 
5
  import io
6
  from PIL import Image
7
  import base64
8
+ import os
9
+ import requests
10
  #client = Client("https://duchaba-friendly-text-moderation.hf.space/--replicas/gffry/")
11
+ #client = Client("https://duchaba-friendly-text-moderation.hf.space/--replicas/6rx2j/")
12
+
13
+ # Load Hugging Face token from environment variable
14
+ HF_TOKEN = os.getenv("HF_TOKEN", "your_default_hf_token")
15
+
16
+ def get_dynamic_endpoint():
17
+ """
18
+ Fetch the dynamic endpoint using the Hugging Face API.
19
+
20
+ Returns:
21
+ str: The current dynamic endpoint.
22
+ """
23
+ api_url = "https://api.huggingface.co/space/duchaba/friendly-text-moderation"
24
+ headers = {"Authorization": f"Bearer {HF_TOKEN}"}
25
+
26
+ response = requests.get(api_url, headers=headers)
27
+ response.raise_for_status() # Raise an error for bad status codes
28
+
29
+ # Extract the endpoint from the response
30
+ data = response.json()
31
+ endpoint = data.get("url")
32
+ return endpoint
33
+
34
 
35
 
36
  def moderate_text(text, safer_value):
37
+ # Fetch the dynamic endpoint
38
+ dynamic_endpoint = get_dynamic_endpoint()
39
+
40
+ # Initialize the client with the dynamic endpoint
41
+ client = Client(dynamic_endpoint, hf_token=HF_TOKEN)
42
  result = client.predict(
43
  text,
44
  safer_value,
fetchURL.txt ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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,
52
+ api_name="/censor_me"
53
+ )
54
+
55
+ # Example structure of the result
56
+ base64_data = result.get('plot', '').split(',')[1]
57
+
58
+ # Decode base64 to bytes
59
+ img_data = base64.b64decode(base64_data)
60
+
61
+ # Convert bytes to PIL Image
62
+ img = Image.open(io.BytesIO(img_data))
63
+
64
+ return result, img
65
+
66
+ # Define the Gradio interface
67
+ demo = gr.Interface(
68
+ fn=moderate_text,
69
+ inputs=[
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"
80
+ )
81
+
82
+ # Customize the CSS
83
+ custom_css = """
84
+ body {
85
+ background-color: #f5f5f5;
86
+ font-family: Arial, sans-serif;
87
+ }
88
+ .gradio-container {
89
+ max-width: 800px;
90
+ margin: auto;
91
+ padding: 20px;
92
+ background-color: white;
93
+ border: 1px solid #ddd;
94
+ border-radius: 8px;
95
+ box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
96
+ }
97
+ .gr-button {
98
+ background-color: #4CAF50;
99
+ color: white;
100
+ }
101
+ .gr-button:hover {
102
+ background-color: #45a049;
103
+ }
104
+ """
105
+
106
+ # Add the custom CSS to the Gradio app
107
+ demo.css = custom_css
108
+
109
+ # Launch the app
110
+ demo.launch()
111
+