Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,107 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
"""
|
| 5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 6 |
-
"""
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
message,
|
| 12 |
-
history: list[tuple[str, str]],
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
messages.append({"role": "user", "content": message})
|
| 27 |
|
| 28 |
-
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
response += token
|
| 40 |
-
yield response
|
| 41 |
|
| 42 |
|
| 43 |
"""
|
| 44 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
"""
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
-
],
|
| 60 |
)
|
| 61 |
|
|
|
|
|
|
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
# from huggingface_hub import InferenceClient
|
| 3 |
+
from transformers import AutoModelForSequenceClassification, AutoConfig, AutoTokenizer
|
| 4 |
+
import torch
|
| 5 |
+
import numpy as np
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
MODEL_NAME = "URaBOT2024/debertaV3_FullFeature"
|
| 9 |
+
|
| 10 |
+
# Load pre-trained models and tokenizers
|
| 11 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME, num_labels = 2)
|
| 12 |
+
config = AutoConfig.from_pretrained(MODEL_NAME)
|
| 13 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 14 |
+
|
| 15 |
+
# Set hardware target for model
|
| 16 |
+
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
|
| 17 |
+
model.to(device)
|
| 18 |
+
model.eval() # Set model to evaluation mode
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def verify(psudo_id, username, display_name, tweet_content, is_verified, likes):
|
| 23 |
+
'''
|
| 24 |
+
Main Endpoint for URaBOT, a POST request that takes in a tweet's data and returns a "bot" score
|
| 25 |
+
|
| 26 |
+
Returns: JSON object {"percent": double}
|
| 27 |
+
|
| 28 |
+
payload:
|
| 29 |
+
"psudo_id": the temporary id of the tweet (as assigned in local HTML from Twitter)
|
| 30 |
+
"username": the profile's username (@tag)
|
| 31 |
+
"display_name": the profiles display name
|
| 32 |
+
"tweet_content": the text content of the tweet
|
| 33 |
+
'''
|
| 34 |
|
| 35 |
+
# #========== Error codes ==========#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
# # Confirm that full payload was sent
|
| 38 |
+
# if 'username' not in request.form:
|
| 39 |
+
# return make_response(jsonify({"error": "Invalid request parameters.", "message" : "No username provided"}), 400)
|
| 40 |
+
|
| 41 |
+
# if 'display_name' not in request.form:
|
| 42 |
+
# return make_response(jsonify({"error": "Invalid request parameters.", "message" : "No display_name provided"}), 400)
|
| 43 |
+
|
| 44 |
+
# if 'tweet_content' not in request.form:
|
| 45 |
+
# return make_response(jsonify({"error": "Invalid request parameters.", "message" : "No tweet_content provided"}), 400)
|
| 46 |
+
|
| 47 |
+
# # Prevent multiple requests for the same tweet
|
| 48 |
+
# if request.form["psudo_id"] in processed_tweets:
|
| 49 |
+
# return make_response(jsonify({"error": "Conflict, tweet is already being/has been processed"}), 409)
|
| 50 |
|
|
|
|
| 51 |
|
| 52 |
+
# #========== Resolve Multiple Requests ==========#
|
| 53 |
|
| 54 |
+
# # Add tweet to internal (backend) process list
|
| 55 |
+
# processed_tweets.append(request.form["psudo_id"])
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
#========== Return Classification ==========#
|
| 59 |
+
|
| 60 |
+
# Process the tweet through the model
|
| 61 |
+
# input = request.form["tweet_content"] + tokenizer.sep_token + request.form["display_name"] + tokenizer.sep_token + request.form["is_verified"] + tokenizer.sep_token + request.form["likes"]
|
| 62 |
+
|
| 63 |
+
input = tweet_content + tokenizer.sep_token + display_name + tokenizer.sep_token + is_verified + tokenizer.sep_token + likes
|
| 64 |
+
tokenized_input = tokenizer(input, return_tensors='pt', padding=True, truncation=True).to(device)
|
| 65 |
+
with torch.no_grad():
|
| 66 |
+
outputs = model(**tokenized_input)
|
| 67 |
+
|
| 68 |
+
# Determine classification
|
| 69 |
+
sigmoid = (1 / (1 + np.exp(-outputs.logits.detach().numpy()))).tolist()[0]
|
| 70 |
+
|
| 71 |
+
# Apply Platt Scaling
|
| 72 |
+
# if USE_PS:
|
| 73 |
+
# sigmoid = [(1/(1+ math.exp(-(A * x + B)))) for x in sigmoid]
|
| 74 |
+
|
| 75 |
+
# Find majority class
|
| 76 |
+
label = np.argmax(outputs.logits.detach().numpy(), axis=-1).item()
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
# Return sigmoid-ish value for classification. Can instead return label for strict 0/1 binary classification
|
| 80 |
+
if label == 0:
|
| 81 |
+
return 1 - sigmoid[0]
|
| 82 |
+
else:
|
| 83 |
+
return sigmoid[1]
|
| 84 |
|
|
|
|
|
|
|
| 85 |
|
| 86 |
|
| 87 |
"""
|
| 88 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 89 |
"""
|
| 90 |
+
# Set up the Gradio Interface
|
| 91 |
+
iface = gr.Interface(
|
| 92 |
+
fn=verify, # Function to process input
|
| 93 |
+
inputs=[gr.inputs.Textbox(label= "Text 1"),
|
| 94 |
+
gr.inputs.Textbox(label= "Text 2"),
|
| 95 |
+
gr.inputs.Textbox(label= "Text 3"),
|
| 96 |
+
gr.inputs.Textbox(label= "Text 4")] # Input type (Textbox for text)
|
| 97 |
+
outputs=gr.outputs.Textbox(), # Output type (Textbox for generated text)
|
| 98 |
+
live=True # Optional: To update the result as you type
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
)
|
| 100 |
|
| 101 |
+
# Launch the API on a specific port
|
| 102 |
+
|
| 103 |
|
| 104 |
if __name__ == "__main__":
|
| 105 |
+
iface.launch(share=True) # share=True will give you a public URL to use the API
|
| 106 |
+
|
| 107 |
+
# demo.launch()
|