Alibrown commited on
Commit
ab96d8b
·
verified ·
1 Parent(s): 8534d1c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -9
app.py CHANGED
@@ -1,6 +1,8 @@
1
  import os
2
- # Setze die Umgebungsvariable, BEVOR streamlit initialisiert wird (Muss ganz oben stehen!)
3
  os.environ["STREAMLIT_GATHER_USAGE_STATS"] = "false"
 
 
4
 
5
  import streamlit as st
6
  import requests
@@ -11,17 +13,19 @@ import base64
11
  import pandas as pd
12
  import zipfile
13
  import PyPDF2
 
14
 
15
  # --- Konfiguration ---
16
  st.set_page_config(page_title="OpenRouter Free Interface", layout="wide", initial_sidebar_state="expanded")
17
  OPENROUTER_API_BASE = "https://openrouter.ai/api/v1"
18
 
 
 
19
  # --- Page Title ---
20
  st.title("💸 OpenRouter Free-Tier Interface")
21
  st.markdown("""
22
  **Willkommen im All-OpenRouter-Free-Interface Deluxe!**
23
  Chatte mit **kostenlosen (Free-Tier)** Modellen über die OpenRouter API.
24
- Alle Modelle unterliegen den OpenRouter-Ratenbegrenzungen.
25
  """)
26
 
27
  # --- Session State Management ---
@@ -30,21 +34,17 @@ if "messages" not in st.session_state:
30
  if "uploaded_content" not in st.session_state:
31
  st.session_state.uploaded_content = None
32
 
33
-
34
  # --- Datei-Verarbeitung ---
35
  def encode_image(image):
36
  buf = io.BytesIO()
37
  image.save(buf, format="JPEG")
38
  return base64.b64encode(buf.getvalue()).decode("utf-8")
39
 
40
-
41
  def process_file(uploaded_file):
42
  file_type = uploaded_file.name.split('.')[-1].lower()
43
  text_exts = ('.txt', '.csv', '.py', '.html', '.js', '.css', '.json', '.xml', '.sql', '.xlsx')
44
-
45
  if file_type in ["jpg", "jpeg", "png"]:
46
  return {"type": "image", "content": Image.open(uploaded_file).convert('RGB')}
47
- # ... (Rest der process_file Funktion, unverändert)
48
  if file_type in ["txt"] + [ext.strip('.') for ext in text_exts if ext not in ('.csv', '.xlsx')]:
49
  return {"type": "text", "content": uploaded_file.read().decode("utf-8", errors="ignore")}
50
  if file_type in ["csv", "xlsx"]:
@@ -73,9 +73,8 @@ def process_file(uploaded_file):
73
  return {"type": "error", "content": "Nicht unterstütztes Dateiformat."}
74
 
75
 
76
- # --- Context-Length Fetch (Ohne Caching für maximale Kompatibilität) ---
77
  def fetch_model_contexts(api_key):
78
- """Lädt alle Modelle + deren context_length."""
79
  if not api_key:
80
  return {}
81
  headers = {"Authorization": f"Bearer {api_key}"}
@@ -173,7 +172,7 @@ def call_openrouter(model, messages, temp, max_tok, key):
173
  "temperature": temp,
174
  "max_tokens": max_tok,
175
  }
176
- # ... (Rest des API Calls)
177
  res = requests.post(f"{OPENROUTER_API_BASE}/chat/completions", headers=headers, data=json.dumps(payload))
178
  if res.status_code == 200:
179
  try:
 
1
  import os
2
+ # FIX 1: Metriken deaktivieren (falls noch nötig)
3
  os.environ["STREAMLIT_GATHER_USAGE_STATS"] = "false"
4
+ # FIX 2: Streamlit Home-Verzeichnis in den beschreibbaren /tmp-Bereich umleiten
5
+ os.environ["STREAMLIT_HOME"] = "/tmp"
6
 
7
  import streamlit as st
8
  import requests
 
13
  import pandas as pd
14
  import zipfile
15
  import PyPDF2
16
+ # os wird jetzt für die Umgebungsvariablen verwendet
17
 
18
  # --- Konfiguration ---
19
  st.set_page_config(page_title="OpenRouter Free Interface", layout="wide", initial_sidebar_state="expanded")
20
  OPENROUTER_API_BASE = "https://openrouter.ai/api/v1"
21
 
22
+ # [Der gesamte Rest Ihres Codes (Page Title, Session State, File Processing, API Calls etc.) bleibt unverändert.]
23
+
24
  # --- Page Title ---
25
  st.title("💸 OpenRouter Free-Tier Interface")
26
  st.markdown("""
27
  **Willkommen im All-OpenRouter-Free-Interface Deluxe!**
28
  Chatte mit **kostenlosen (Free-Tier)** Modellen über die OpenRouter API.
 
29
  """)
30
 
31
  # --- Session State Management ---
 
34
  if "uploaded_content" not in st.session_state:
35
  st.session_state.uploaded_content = None
36
 
 
37
  # --- Datei-Verarbeitung ---
38
  def encode_image(image):
39
  buf = io.BytesIO()
40
  image.save(buf, format="JPEG")
41
  return base64.b64encode(buf.getvalue()).decode("utf-8")
42
 
 
43
  def process_file(uploaded_file):
44
  file_type = uploaded_file.name.split('.')[-1].lower()
45
  text_exts = ('.txt', '.csv', '.py', '.html', '.js', '.css', '.json', '.xml', '.sql', '.xlsx')
 
46
  if file_type in ["jpg", "jpeg", "png"]:
47
  return {"type": "image", "content": Image.open(uploaded_file).convert('RGB')}
 
48
  if file_type in ["txt"] + [ext.strip('.') for ext in text_exts if ext not in ('.csv', '.xlsx')]:
49
  return {"type": "text", "content": uploaded_file.read().decode("utf-8", errors="ignore")}
50
  if file_type in ["csv", "xlsx"]:
 
73
  return {"type": "error", "content": "Nicht unterstütztes Dateiformat."}
74
 
75
 
76
+ # --- Context-Length Fetch ---
77
  def fetch_model_contexts(api_key):
 
78
  if not api_key:
79
  return {}
80
  headers = {"Authorization": f"Bearer {api_key}"}
 
172
  "temperature": temp,
173
  "max_tokens": max_tok,
174
  }
175
+
176
  res = requests.post(f"{OPENROUTER_API_BASE}/chat/completions", headers=headers, data=json.dumps(payload))
177
  if res.status_code == 200:
178
  try: