|
import gradio as gr |
|
import requests |
|
import time |
|
import os |
|
|
|
|
|
class AgreementClassifier: |
|
def __init__(self, api_token, api_url, backoff_factor=1): |
|
self.api_token = api_token |
|
self.api_url = api_url |
|
self.headers = {"Authorization": f"Bearer {self.api_token}"} |
|
self.backoff_factor = backoff_factor |
|
|
|
def query(self, payload): |
|
retries = 0 |
|
while True: |
|
response = requests.post(self.api_url, headers=self.headers, json=payload) |
|
if response.status_code == 503: |
|
retries += 1 |
|
wait_time = self.backoff_factor * (2 ** (retries - 1)) |
|
print(f"503 Service Unavailable. Retrying in {wait_time} seconds...") |
|
time.sleep(wait_time) |
|
else: |
|
response.raise_for_status() |
|
return response.json() |
|
|
|
def classify_text_topic(self, input_text): |
|
result = self.query( |
|
{ |
|
"inputs": input_text, |
|
"parameters": {}, |
|
} |
|
) |
|
return result |
|
|
|
|
|
API_TOKEN = os.getenv("API_TOKEN") |
|
API_URL = os.getenv("API_URL") |
|
classifier = AgreementClassifier(API_TOKEN, API_URL) |
|
|
|
|
|
color_mapping = { |
|
"Ablehnung": "red", |
|
"Neutral": "yellow", |
|
"Zustimmung": "green" |
|
} |
|
|
|
|
|
def classify_text(text): |
|
|
|
predictions = classifier.classify_text_topic(text) |
|
|
|
|
|
predicted_label = max(predictions, key=lambda x: x['score'])['label'] |
|
|
|
|
|
return f'<div style="background-color: {color_mapping[predicted_label]}; padding: 10px; border-radius: 5px;">{predicted_label}</div>' |
|
|
|
|
|
with gr.Blocks(css=".gradio-container { max-width: 400px; margin: auto; }") as interface: |
|
gr.Markdown("# ePA Classifier") |
|
gr.Markdown("Gib einen Satz oder Text ein, der in 'Ablehnung', 'Neutral', oder 'Zustimmung' klassifiziert werden soll.") |
|
|
|
|
|
text_input = gr.Textbox(lines=1, placeholder="Hier Text...") |
|
|
|
|
|
submit_btn = gr.Button("Klassifizieren") |
|
|
|
|
|
result_output = gr.HTML(value="<div style='color:gray;'>Das Ergebnis wird hier angezeigt</div>") |
|
|
|
|
|
submit_btn.click(fn=classify_text, inputs=text_input, outputs=result_output) |
|
|
|
|
|
text_input.submit(fn=classify_text, inputs=text_input, outputs=result_output) |
|
|
|
|
|
interface.launch() |
|
|