import gradio as gr
import requests
import os
class Client:
def __init__(self):
self.url = os.getenv("SERVICE_IP")
self.headers = {
"x-api-key": os.getenv("APIKEY")
}
def detect(self, document):
data = {
"document": document,
"version": "no-version",
"multilingual": False
}
if len(document) > 3000:
return "Document is too long for this demo"
response = requests.post(self.url, json=data, headers=self.headers)
response = response.json()
documents = response.get("documents")
res = ""
len_ai=0
len_human=0
for doc in documents:
if "AI" in doc["classification"]:
len_ai += len(doc["original_paragraph"])
res += f'{doc["original_paragraph"]}'
else:
len_human += len(doc["original_paragraph"])
res += f'{doc["original_paragraph"]}'
res += f"
The above text has a probability of {len_ai/(len_ai+len_human)*100:.2f}% to be AI."
return res
client = Client()
def respond(
message,
):
return client.detect(message)
description = '🤖 Demo for [ImBD](https://github.com/Jiaqi-Chen-00/ImBD)
'\
'We slice the input text into segments of 300 characters each, ' \
'supporting a maximum text length of 3000 characters.
'\
'The result for each segment is indicated by green for human and red for AI on the right.'
demo = gr.Interface(
fn=respond,
inputs=[
gr.Textbox(label="Input Message"),
],
outputs="html",
description=description
)
if __name__ == "__main__":
demo.launch()