Spaces:
Running
Running
| 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() | |