Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,10 +7,10 @@ import base64
|
|
| 7 |
import pandas as pd
|
| 8 |
import zipfile
|
| 9 |
import PyPDF2
|
| 10 |
-
import os
|
| 11 |
|
| 12 |
# --- Konfiguration ---
|
| 13 |
-
st.set_page_config(page_title="OpenRouter Free Interface", layout="wide")
|
| 14 |
OPENROUTER_API_BASE = "https://openrouter.ai/api/v1"
|
| 15 |
|
| 16 |
# --- Page Title ---
|
|
@@ -36,6 +36,7 @@ def encode_image(image):
|
|
| 36 |
|
| 37 |
|
| 38 |
def process_file(uploaded_file):
|
|
|
|
| 39 |
file_type = uploaded_file.name.split('.')[-1].lower()
|
| 40 |
text_exts = ('.txt', '.csv', '.py', '.html', '.js', '.css', '.json', '.xml', '.sql', '.xlsx')
|
| 41 |
|
|
@@ -74,7 +75,8 @@ def process_file(uploaded_file):
|
|
| 74 |
return {"type": "error", "content": "Nicht unterstütztes Dateiformat."}
|
| 75 |
|
| 76 |
|
| 77 |
-
# --- Context-Length Fetch ---
|
|
|
|
| 78 |
def fetch_model_contexts(api_key):
|
| 79 |
"""Lädt alle Modelle + deren context_length."""
|
| 80 |
if not api_key:
|
|
@@ -90,7 +92,7 @@ def fetch_model_contexts(api_key):
|
|
| 90 |
contexts[mid] = ctx
|
| 91 |
return contexts
|
| 92 |
except Exception as e:
|
| 93 |
-
st.warning
|
| 94 |
return {}
|
| 95 |
|
| 96 |
|
|
@@ -111,23 +113,32 @@ with st.sidebar:
|
|
| 111 |
|
| 112 |
model = st.selectbox("Wähle ein Modell", FREE_MODEL_LIST, index=0)
|
| 113 |
|
| 114 |
-
# Context automatisch anpassen
|
| 115 |
model_contexts = fetch_model_contexts(api_key)
|
| 116 |
default_ctx = model_contexts.get(model, 4096)
|
| 117 |
|
| 118 |
temperature = st.slider("Temperature", 0.0, 1.0, 0.7)
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
st.caption(f"🔢 Model Context Length: {default_ctx}")
|
|
|
|
|
|
|
| 121 |
|
| 122 |
-
|
|
|
|
| 123 |
st.session_state.messages = []
|
| 124 |
st.session_state.uploaded_content = None
|
| 125 |
-
st.
|
|
|
|
| 126 |
|
| 127 |
st.markdown("""
|
| 128 |
---
|
| 129 |
-
🧠 **Hinweis:**
|
| 130 |
-
Dein API-Key wird nur **lokal** verwendet.
|
| 131 |
""")
|
| 132 |
|
| 133 |
|
|
@@ -137,6 +148,9 @@ uploaded_file = st.file_uploader("Upload File (optional)",
|
|
| 137 |
|
| 138 |
if uploaded_file and st.session_state.uploaded_content is None:
|
| 139 |
st.session_state.uploaded_content = process_file(uploaded_file)
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
if st.session_state.uploaded_content:
|
| 142 |
processed = st.session_state.uploaded_content
|
|
@@ -147,6 +161,8 @@ if st.session_state.uploaded_content:
|
|
| 147 |
st.text_area("File Preview", processed["content"], height=150)
|
| 148 |
elif processed["type"] == "error":
|
| 149 |
st.error(processed["content"])
|
|
|
|
|
|
|
| 150 |
if st.button("❌ Remove Attachment"):
|
| 151 |
st.session_state.uploaded_content = None
|
| 152 |
st.experimental_rerun()
|
|
@@ -204,11 +220,13 @@ if prompt := st.chat_input("Deine Nachricht..."):
|
|
| 204 |
content = st.session_state.uploaded_content
|
| 205 |
if content["type"] == "image":
|
| 206 |
base64_img = encode_image(content["content"])
|
|
|
|
| 207 |
messages[-1]["content"] = [
|
| 208 |
{"type": "text", "text": prompt},
|
| 209 |
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{base64_img}"}}
|
| 210 |
]
|
| 211 |
elif content["type"] == "text":
|
|
|
|
| 212 |
messages[-1]["content"] += f"\n\n[Attached File Content]\n{content['content']}"
|
| 213 |
|
| 214 |
with st.chat_message("assistant"):
|
|
@@ -219,4 +237,4 @@ if prompt := st.chat_input("Deine Nachricht..."):
|
|
| 219 |
st.session_state.messages.append({"role": "assistant", "content": reply})
|
| 220 |
except Exception as e:
|
| 221 |
st.error(str(e))
|
| 222 |
-
st.session_state.messages.append({"role": "assistant", "content": f"❌ {str(e)}"})
|
|
|
|
| 7 |
import pandas as pd
|
| 8 |
import zipfile
|
| 9 |
import PyPDF2
|
| 10 |
+
import os
|
| 11 |
|
| 12 |
# --- Konfiguration ---
|
| 13 |
+
st.set_page_config(page_title="OpenRouter Free Interface", layout="wide", initial_sidebar_state="expanded")
|
| 14 |
OPENROUTER_API_BASE = "https://openrouter.ai/api/v1"
|
| 15 |
|
| 16 |
# --- Page Title ---
|
|
|
|
| 36 |
|
| 37 |
|
| 38 |
def process_file(uploaded_file):
|
| 39 |
+
# Ihr robuster File Manager ist perfekt
|
| 40 |
file_type = uploaded_file.name.split('.')[-1].lower()
|
| 41 |
text_exts = ('.txt', '.csv', '.py', '.html', '.js', '.css', '.json', '.xml', '.sql', '.xlsx')
|
| 42 |
|
|
|
|
| 75 |
return {"type": "error", "content": "Nicht unterstütztes Dateiformat."}
|
| 76 |
|
| 77 |
|
| 78 |
+
# --- Context-Length Fetch (MIT CACHING) ---
|
| 79 |
+
@st.cache_data(ttl=3600) # Caching für 1 Stunde, um API-Calls zu sparen
|
| 80 |
def fetch_model_contexts(api_key):
|
| 81 |
"""Lädt alle Modelle + deren context_length."""
|
| 82 |
if not api_key:
|
|
|
|
| 92 |
contexts[mid] = ctx
|
| 93 |
return contexts
|
| 94 |
except Exception as e:
|
| 95 |
+
# Hier kein st.warning, da es den Cache ungültig machen könnte, wenn API-Key noch nicht da ist
|
| 96 |
return {}
|
| 97 |
|
| 98 |
|
|
|
|
| 113 |
|
| 114 |
model = st.selectbox("Wähle ein Modell", FREE_MODEL_LIST, index=0)
|
| 115 |
|
| 116 |
+
# Context automatisch anpassen (durch Caching nur bei Bedarf neu geladen)
|
| 117 |
model_contexts = fetch_model_contexts(api_key)
|
| 118 |
default_ctx = model_contexts.get(model, 4096)
|
| 119 |
|
| 120 |
temperature = st.slider("Temperature", 0.0, 1.0, 0.7)
|
| 121 |
+
# Slider Maxwert auf Context-Länge des Modells setzen, Min-Output-Tokens auf 512
|
| 122 |
+
max_tokens = st.slider(
|
| 123 |
+
f"Max Output Tokens (max {default_ctx})",
|
| 124 |
+
1,
|
| 125 |
+
min(default_ctx, 32000),
|
| 126 |
+
min(512, default_ctx)
|
| 127 |
+
)
|
| 128 |
st.caption(f"🔢 Model Context Length: {default_ctx}")
|
| 129 |
+
|
| 130 |
+
st.markdown("---")
|
| 131 |
|
| 132 |
+
# Verbesserter Reset-Button
|
| 133 |
+
if st.button("🔄 Chat Reset (Full)"):
|
| 134 |
st.session_state.messages = []
|
| 135 |
st.session_state.uploaded_content = None
|
| 136 |
+
# Rerun, um st.file_uploader zu leeren und die App in den Startzustand zu versetzen
|
| 137 |
+
st.experimental_rerun()
|
| 138 |
|
| 139 |
st.markdown("""
|
| 140 |
---
|
| 141 |
+
🧠 **Hinweis:** Dein API-Key wird nur **lokal** verwendet, um Anfragen an OpenRouter zu authentifizieren.
|
|
|
|
| 142 |
""")
|
| 143 |
|
| 144 |
|
|
|
|
| 148 |
|
| 149 |
if uploaded_file and st.session_state.uploaded_content is None:
|
| 150 |
st.session_state.uploaded_content = process_file(uploaded_file)
|
| 151 |
+
# Rerun, um die Vorschau sofort zu zeigen
|
| 152 |
+
st.experimental_rerun()
|
| 153 |
+
|
| 154 |
|
| 155 |
if st.session_state.uploaded_content:
|
| 156 |
processed = st.session_state.uploaded_content
|
|
|
|
| 161 |
st.text_area("File Preview", processed["content"], height=150)
|
| 162 |
elif processed["type"] == "error":
|
| 163 |
st.error(processed["content"])
|
| 164 |
+
|
| 165 |
+
# Anhang einzeln entfernen
|
| 166 |
if st.button("❌ Remove Attachment"):
|
| 167 |
st.session_state.uploaded_content = None
|
| 168 |
st.experimental_rerun()
|
|
|
|
| 220 |
content = st.session_state.uploaded_content
|
| 221 |
if content["type"] == "image":
|
| 222 |
base64_img = encode_image(content["content"])
|
| 223 |
+
# OpenRouter Multimodalität (OpenAI-Schema)
|
| 224 |
messages[-1]["content"] = [
|
| 225 |
{"type": "text", "text": prompt},
|
| 226 |
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{base64_img}"}}
|
| 227 |
]
|
| 228 |
elif content["type"] == "text":
|
| 229 |
+
# Text-Dateien einfach dem letzten Prompt anhängen
|
| 230 |
messages[-1]["content"] += f"\n\n[Attached File Content]\n{content['content']}"
|
| 231 |
|
| 232 |
with st.chat_message("assistant"):
|
|
|
|
| 237 |
st.session_state.messages.append({"role": "assistant", "content": reply})
|
| 238 |
except Exception as e:
|
| 239 |
st.error(str(e))
|
| 240 |
+
st.session_state.messages.append({"role": "assistant", "content": f"❌ {str(e)}"})
|