File size: 1,888 Bytes
7e4ac6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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'<span style="color:red;">{doc["original_paragraph"]}</span>'
            else:
                len_human += len(doc["original_paragraph"])
                res += f'<span style="color:green;">{doc["original_paragraph"]}</span>'
                
        res += f"<br><br>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 = '<span style="font-size: 30px;">🤖 Demo for [ImBD](https://github.com/Jiaqi-Chen-00/ImBD) <br></span>'\
    '<span style="font-size: 18px;">We slice the input text into segments of 300 characters each, ' \
    'supporting a maximum text length of 3000 characters.<br>'\
    'The result for each segment is indicated by green for human and red for AI on the right.</span>'


demo = gr.Interface(
    fn=respond,
    inputs=[
        gr.Textbox(label="Input Message"),
    ],
    outputs="html",
    description=description
)


if __name__ == "__main__":
    demo.launch()