Spaces:
Runtime error
Runtime error
Commit
·
cb907c6
1
Parent(s):
4fde4b7
update
Browse files- requirements.txt +7 -1
- wap.py +96 -0
requirements.txt
CHANGED
|
@@ -1 +1,7 @@
|
|
| 1 |
-
huggingface_hub==0.25.2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
huggingface_hub==0.25.2
|
| 2 |
+
transformers
|
| 3 |
+
torch
|
| 4 |
+
requests
|
| 5 |
+
Pillow
|
| 6 |
+
open_clip_torch
|
| 7 |
+
ftfy
|
wap.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextStreamer
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Modell und Tokenizer laden
|
| 6 |
+
model_name = "Mario12355/llama_3.1_20.11_fini_dpo"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
+
model_name,
|
| 10 |
+
torch_dtype=torch.float16,
|
| 11 |
+
device_map="auto"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
# Dein Alpaca-Prompt Template
|
| 15 |
+
alpaca_prompt = """### Instruction:
|
| 16 |
+
{}
|
| 17 |
+
|
| 18 |
+
### Input:
|
| 19 |
+
{}
|
| 20 |
+
|
| 21 |
+
### Response:
|
| 22 |
+
{}"""
|
| 23 |
+
|
| 24 |
+
def translate(
|
| 25 |
+
message,
|
| 26 |
+
history,
|
| 27 |
+
direction,
|
| 28 |
+
max_tokens=128
|
| 29 |
+
):
|
| 30 |
+
# Richtung bestimmen und Anweisung formatieren
|
| 31 |
+
if direction == "hochdeutsch_to_schwaebisch":
|
| 32 |
+
instruction = "Übersetze den hochdeutschen Text ins Schwäbische. Achte auf eine sinnvolle und korrekte Satzbildung!"
|
| 33 |
+
elif direction == "schwaebisch_to_hochdeutsch":
|
| 34 |
+
instruction = "Übersetze den schwäbischen Text ins Hochdeutsche. Achte auf eine sinnvolle und korrekte Satzbildung!"
|
| 35 |
+
else:
|
| 36 |
+
raise ValueError("Ungültige Übersetzungsrichtung")
|
| 37 |
+
|
| 38 |
+
# Input für das Modell vorbereiten
|
| 39 |
+
inputs = tokenizer(
|
| 40 |
+
[alpaca_prompt.format(instruction, message, "")],
|
| 41 |
+
return_tensors="pt"
|
| 42 |
+
).to(model.device)
|
| 43 |
+
|
| 44 |
+
# Streaming-Generator erstellen
|
| 45 |
+
response = ""
|
| 46 |
+
streamer = TextStreamer(tokenizer)
|
| 47 |
+
|
| 48 |
+
# Generator-Konfiguration
|
| 49 |
+
generation_config = {
|
| 50 |
+
"max_new_tokens": max_tokens,
|
| 51 |
+
"do_sample": True,
|
| 52 |
+
"temperature": 0.7,
|
| 53 |
+
"top_p": 0.95,
|
| 54 |
+
"streamer": streamer,
|
| 55 |
+
**inputs
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
# Text generieren und streamen
|
| 59 |
+
for output in model.generate(**generation_config):
|
| 60 |
+
# Token decodieren und zum Response hinzufügen
|
| 61 |
+
new_text = tokenizer.decode(output, skip_special_tokens=True)
|
| 62 |
+
if new_text != response: # Nur neue Tokens ausgeben
|
| 63 |
+
yield new_text
|
| 64 |
+
|
| 65 |
+
# Gradio Interface erstellen
|
| 66 |
+
demo = gr.ChatInterface(
|
| 67 |
+
translate,
|
| 68 |
+
additional_inputs=[
|
| 69 |
+
gr.Radio(
|
| 70 |
+
choices=["hochdeutsch_to_schwaebisch", "schwaebisch_to_hochdeutsch"],
|
| 71 |
+
value="hochdeutsch_to_schwaebisch",
|
| 72 |
+
label="Übersetzungsrichtung"
|
| 73 |
+
),
|
| 74 |
+
gr.Slider(
|
| 75 |
+
minimum=32,
|
| 76 |
+
maximum=2048,
|
| 77 |
+
value=128,
|
| 78 |
+
step=32,
|
| 79 |
+
label="Maximale Anzahl neuer Tokens"
|
| 80 |
+
)
|
| 81 |
+
],
|
| 82 |
+
title="Schwäbisch Übersetzer",
|
| 83 |
+
description="""Dieser Übersetzer kann Texte zwischen Hochdeutsch und Schwäbisch übersetzen.
|
| 84 |
+
Wählen Sie die gewünschte Übersetzungsrichtung und geben Sie Ihren Text ein.""",
|
| 85 |
+
examples=[
|
| 86 |
+
["Guten Tag, wie geht es Ihnen?", "hochdeutsch_to_schwaebisch"],
|
| 87 |
+
["Griaß Gott, wie goht's dir?", "schwaebisch_to_hochdeutsch"]
|
| 88 |
+
]
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
if __name__ == "__main__":
|
| 92 |
+
demo.launch(
|
| 93 |
+
share=True,
|
| 94 |
+
show_error=True,
|
| 95 |
+
cache_examples=True
|
| 96 |
+
)
|