Download app.py from transline/Checker: direct link, hf CLI and curl.
- Browser
- Download file 2.93 kB
-
https://huggingface.co/spaces/transline/Checker/resolve/main/app.py
- Command line
-
hf download hf://spaces/transline/Checker/app.py
-
curl -L -o app.py https://huggingface.co/spaces/transline/Checker/resolve/main/app.py
2.93 kB
| import gradio as gr | |
| import numpy as np | |
| from transformers import Trainer, AutoTokenizer, XLMRobertaForSequenceClassification | |
| import torch | |
| import pickle | |
| import re | |
| import PyPDF2 | |
| import docx | |
| import xml.etree.ElementTree as ET | |
| def checker(content): | |
| #if isinstance(content, bytes):#falls es ein Byte-String ist | |
| # content = content.decode('utf-8') | |
| sentences = re.split(r"(?<=[!.;?:])\s+(?![a-zà-öø-ÿα-ωа-яא-תء-ي])", content) | |
| model_name = "abischoff/test-trainer" | |
| tokenizer = AutoTokenizer.from_pretrained(model_name) | |
| model = XLMRobertaForSequenceClassification.from_pretrained(model_name,num_labels=1) | |
| sum = 0.0 | |
| bias = -0.0666 | |
| for dic in sentences: | |
| inputs = tokenizer(dic.strip(), return_tensors="pt") | |
| with torch.no_grad(): | |
| logits = model(**inputs).logits.flatten()[0].numpy()+bias | |
| logits = np.clip(logits,0,1) | |
| sum += logits | |
| res = sum / len(sentences) | |
| return res | |
| # Funktion zur Verarbeitung der hochgeladenen Datei | |
| def process_file(file): | |
| if file: | |
| content = read_file(file) | |
| if content == "": | |
| return "Datei konnte nicht gelesen werden" | |
| else: | |
| res = checker(content) | |
| return res | |
| return "Fehler bei der Verarbeitung" | |
| # lese die Datei ein | |
| def read_file(file): | |
| content = "" | |
| if ".pdf" in file.name: | |
| # creating a pdf reader object | |
| reader = PyPDF2.PdfReader(file.name) | |
| counter = 0 | |
| while counter < len(reader.pages): | |
| content += reader.pages[counter].extract_text() | |
| counter += 1 | |
| elif ".docx" in file.name: | |
| doc = docx.Document(file.name) | |
| for para in doc.paragraphs: | |
| content += para.text | |
| elif ".xml" in file.name: | |
| tree = ET.parse(file.name) | |
| root = tree.getroot() | |
| for text in root.itertext(): | |
| if text != "": | |
| content += text | |
| else: | |
| with open(file.name, 'r', encoding='utf-8') as f: | |
| content = f.read() | |
| #with open(file.name, 'rb') as f: | |
| # content = pickle.load(f) | |
| print(content) | |
| return content | |
| def is_valid_file(file_path: str, file_types: list[str]) -> bool: | |
| file_extension = os.path.splitext(file_path)[1] | |
| return file_extension in file_types | |
| # Gradio Interface | |
| with gr.Blocks(css=".gr-box {border-radius: 8px;}") as demo: | |
| # App-Titel | |
| gr.Markdown( | |
| """ | |
| ## 📝 Checker | |
| Ziehe deine Datei hierhin und lasse den Quality Score errechnen! | |
| """ | |
| ) | |
| # Drag & Drop Datei-Upload | |
| with gr.Row(): | |
| with gr.Column(): | |
| file_input = gr.File(label="Datei hochladen", file_types=['file'], interactive=True) | |
| with gr.Column(): | |
| output_text = gr.Textbox(label="Dateiinhalt", lines=10) | |
| # Callback zum Verarbeiten der Datei | |
| file_input.change(fn=process_file, inputs=[file_input], outputs=[output_text]) | |
| # App starten | |
| demo.launch() |