Spaces:
Build error
Build error
fix app.py
Browse files
app.py
CHANGED
|
@@ -4,26 +4,38 @@ import gradio as gr
|
|
| 4 |
from transformers import pipeline
|
| 5 |
from huggingface_hub import login
|
| 6 |
|
| 7 |
-
load_dotenv()
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
login(token=hf_token)
|
| 13 |
-
|
| 14 |
-
raise ValueError("HUGGING_FACE_TOKEN is not set in the environment variables")
|
| 15 |
|
| 16 |
def classify_text(text):
|
| 17 |
pipe = pipeline("text-classification", model="meta-llama/Prompt-Guard-86M")
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
demo = gr.Interface(
|
| 22 |
-
fn=classify_text,
|
| 23 |
-
inputs="text",
|
| 24 |
-
outputs="label",
|
| 25 |
-
title="Prompt-Guard-86M Text Classification"
|
| 26 |
-
)
|
| 27 |
|
| 28 |
if __name__ == "__main__":
|
| 29 |
-
|
|
|
|
|
|
|
|
|
| 4 |
from transformers import pipeline
|
| 5 |
from huggingface_hub import login
|
| 6 |
|
|
|
|
| 7 |
|
| 8 |
+
def setup_environment():
|
| 9 |
+
load_dotenv()
|
| 10 |
+
hf_token = os.getenv("HUGGING_FACE_TOKEN")
|
| 11 |
+
if not hf_token:
|
| 12 |
+
raise ValueError("HUGGING_FACE_TOKEN is not set in the environment variables")
|
| 13 |
login(token=hf_token)
|
| 14 |
+
|
|
|
|
| 15 |
|
| 16 |
def classify_text(text):
|
| 17 |
pipe = pipeline("text-classification", model="meta-llama/Prompt-Guard-86M")
|
| 18 |
+
results = pipe(text, top_k=None)
|
| 19 |
+
|
| 20 |
+
formatted_results = {result["label"]: result["score"] for result in results}
|
| 21 |
+
top_label = max(results, key=lambda x: x["score"])["label"]
|
| 22 |
+
|
| 23 |
+
return formatted_results, top_label
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def create_interface():
|
| 27 |
+
return gr.Interface(
|
| 28 |
+
fn=classify_text,
|
| 29 |
+
inputs="text",
|
| 30 |
+
outputs=[
|
| 31 |
+
gr.Label(label="Classification Results", num_top_classes=3),
|
| 32 |
+
gr.Textbox(label="Top Classification"),
|
| 33 |
+
],
|
| 34 |
+
title="Prompt-Guard-86M Text Classification",
|
| 35 |
+
)
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
if __name__ == "__main__":
|
| 39 |
+
setup_environment()
|
| 40 |
+
demo = create_interface()
|
| 41 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|