File size: 2,926 Bytes
e3156ed dc86725 8bc5938 dc86725 78493d5 6b02477 55ad0fe 967fdb2 dc86725 5a4245c 9cb773d dc86725 eaf684d dc86725 4ac784e 1cb8641 dc86725 e3156ed ed4cd11 30d7363 78493d5 30d7363 f5f002e 55ad0fe 4d46e2d 6b02477 55ad0fe 78493d5 71cea0c 6b02477 30d7363 e3156ed dfd9319 e3156ed 6071734 e3156ed 8b5d8bc e3156ed b03a9d6 e3156ed 6071734 5f8f4e9 e3156ed | 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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | 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() |