Spaces:
Sleeping
Sleeping
Download src/streamlit_app.py from sorahaja/ML_translater: direct link, hf CLI and curl.
- Browser
- Download file 2.92 kB
-
https://huggingface.co/spaces/sorahaja/ML_translater/resolve/main/src/streamlit_app.py
- Command line
-
hf download hf://spaces/sorahaja/ML_translater/src/streamlit_app.py
-
curl -L -o streamlit_app.py https://huggingface.co/spaces/sorahaja/ML_translater/resolve/main/src/streamlit_app.py
2.92 kB
| import streamlit as st | |
| import os | |
| from transformers import AutoTokenizer, AutoModelForSeq2SeqLM | |
| st.title("✨ KI-Assistent für Einfache Sprache") | |
| # --- MODELL DIREKT IM FRONTEND LADEN (Kein separates Backend nötig!) --- | |
| MODEL_PATH = "sorahaja/test" | |
| # Verhindert, dass das Modell bei jedem Klick neu geladen wird | |
| def load_model(): | |
| with st.spinner("🚀 ML-Modell wird geladen... Bitte kurz warten..."): | |
| try: | |
| tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH) | |
| model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_PATH) | |
| return tokenizer, model, True | |
| except Exception as e: | |
| st.error(f"Fehler beim Laden des Modells: {e}") | |
| return None, None, False | |
| tokenizer, model, HAS_MODEL = load_model() | |
| if HAS_MODEL: | |
| st.sidebar.success("✅ Modell aktiv") | |
| else: | |
| st.sidebar.warning("ℹ️ Simulations-Modus aktiv") | |
| # --- UI ELEMENTE --- | |
| MAX_CHARACTERS = 120 | |
| complex_text = st.text_area( | |
| "Komplexer Ausgangstext:", | |
| height=150, | |
| max_chars=MAX_CHARACTERS, | |
| placeholder="Füge hier deinen Text ein..." | |
| ) | |
| st.sidebar.subheader("⚙️ Übersetzungsparameter anpassen") | |
| temperature = st.sidebar.slider("Kreativität (Temperature):", 0.1, 1.5, 0.7, step=0.1) | |
| max_length = st.sidebar.slider("Maximale Länge (Tokens):", 50, 512, 256, step=10) | |
| if st.button("Text übersetzen / Wiederholen 🔄"): | |
| if complex_text.strip() == "": | |
| st.warning("Bitte gib zuerst einen Text ein!") | |
| else: | |
| with st.spinner("Modell übersetzt..."): | |
| if HAS_MODEL: | |
| try: | |
| input_text = "simplify to plain German: " + complex_text | |
| inputs = tokenizer(input_text, return_tensors="pt", max_length=512, truncation=True) | |
| outputs = model.generate( | |
| **inputs, | |
| max_length=max_length, | |
| temperature=temperature, | |
| do_sample=True if temperature > 0.1 else False, | |
| num_beams=1 if temperature > 0.1 else 4, | |
| early_stopping=True | |
| ) | |
| simplified_text = tokenizer.decode(outputs[0], skip_special_tokens=True) | |
| st.subheader("Vereinfachter Text:") | |
| st.success(simplified_text) | |
| except Exception as e: | |
| st.error(f"Fehler bei der Generierung: {e}") | |
| else: | |
| # Simulation als Fallback | |
| if "Sport" in complex_text: | |
| dummy_output = "Sport baut als intensive körperliche Betätigung Aggressionen ab..." | |
| else: | |
| dummy_output = f"[Simulation]: {complex_text.lower()}" | |
| st.subheader("Vereinfachter Text (Simulation):") | |
| st.success(dummy_output) |