Spaces:
Sleeping
Sleeping
# app.py | |
import streamlit as st | |
import os | |
import faiss | |
import pickle | |
from sentence_transformers import SentenceTransformer | |
from groq import Groq | |
from dotenv import load_dotenv | |
import re # Import regular expressions for expand_query_with_llm_app | |
# --- Page Configuration (MUST BE THE FIRST STREAMLIT COMMAND) --- | |
st.set_page_config(page_title="RAG BITS Tutor", page_icon="🎓") # Set page title and icon | |
# --- Konfiguration und Modell-Laden (am besten ausserhalb von Funktionen, um Caching zu nutzen) --- | |
# Wichtig für das Caching von grossen Modellen und Daten | |
def load_models_and_data(): | |
# Lade Umgebungsvariablen (falls .env Datei im Space vorhanden ist) | |
load_dotenv() | |
groq_api_key_app = os.getenv("GROQ_API_KEY") # Stelle sicher, dass der Key im Space verfügbar ist (siehe Schritt 3) | |
# Pfade zum Index und den Chunks | |
output_folder = "faiss_index_bits" # Muss im HF Space vorhanden sein | |
index_path = os.path.join(output_folder, "bits_tutor.index") | |
chunks_path = os.path.join(output_folder, "bits_chunks.pkl") | |
# Lade FAISS Index | |
if not os.path.exists(index_path): | |
st.error(f"FAISS Index nicht gefunden unter: {index_path}") # This is a Streamlit command | |
return None, None, None, None | |
index_loaded = faiss.read_index(index_path) | |
# Lade Chunks | |
if not os.path.exists(chunks_path): | |
st.error(f"Chunks-Datei nicht gefunden unter: {chunks_path}") # This is a Streamlit command | |
return None, None, None, None | |
with open(chunks_path, "rb") as f: | |
chunks_loaded = pickle.load(f) | |
# Lade Embedding-Modell | |
embedding_model_name_app = "Sahajtomar/German-semantic" | |
embedding_model_loaded = SentenceTransformer(embedding_model_name_app) | |
# Initialisiere Groq Client | |
if not groq_api_key_app: | |
st.error("GROQ_API_KEY nicht gefunden. Bitte im Hugging Face Space als Secret hinzufügen.") # This is a Streamlit command | |
return None, None, None, None | |
groq_client_loaded = Groq(api_key=groq_api_key_app) | |
return index_loaded, chunks_loaded, embedding_model_loaded, groq_client_loaded | |
# Lade Modelle und Daten beim Start der App | |
# Wichtig: Die Funktion load_models_and_data() verwendet st.error(), was ein Streamlit-Befehl ist. | |
# Daher muss st.set_page_config() VOR dem ersten möglichen Aufruf von st.error() stehen. | |
faiss_index, chunks_data, embedding_model, groq_client = load_models_and_data() | |
# ... (Rest deines app.py Skripts bleibt gleich) ... | |
# --- Streamlit UI (kommt nach load_models_and_data) --- | |
st.title("🎓 RAG Study Tutor for Business IT Strategy") | |
# ... etc. ... |