Spaces:
Running
Running
| import gradio as gr | |
| import os | |
| from langchain_community.document_loaders import PyPDFLoader | |
| from langchain_text_splitters import RecursiveCharacterTextSplitter | |
| from langchain_huggingface import HuggingFaceEmbeddings | |
| from langchain_community.vectorstores import FAISS | |
| from openai import OpenAI | |
| # 1. OPENROUTER SETUP | |
| OPENROUTER_API_KEY = os.environ.get("OPENROUTER_API_KEY") | |
| client = OpenAI( | |
| base_url="https://openrouter.ai/api/v1", | |
| api_key=OPENROUTER_API_KEY, | |
| default_headers={ | |
| "HTTP-Referer": "https://huggingface.co/spaces", | |
| "X-Title": "Aya-Interface-Pro", | |
| } | |
| ) if OPENROUTER_API_KEY else None | |
| # LOGBUCH INITIALISIEREN | |
| interaction_counter = 0 | |
| # 2. DATENBANK-LOGIK | |
| def load_book(): | |
| files = [f for f in os.listdir('.') if f.lower().endswith('.pdf')] | |
| if not files: return None | |
| loader = PyPDFLoader(files[0]) | |
| docs = loader.load() | |
| splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200) | |
| texts = splitter.split_documents(docs) | |
| embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2") | |
| return FAISS.from_documents(texts, embeddings) | |
| vector_db = load_book() | |
| # 3. CHAT-LOGIK | |
| def ask_aya(message, history): | |
| global interaction_counter | |
| if not vector_db: return "Aya synchronisiert das Memorandum... einen Moment." | |
| if not client: return "Fehler: API-Key fehlt." | |
| try: | |
| interaction_counter += 1 | |
| docs = vector_db.similarity_search(message, k=5) | |
| context = "\n".join([d.page_content for d in docs]) | |
| shop_link = "https://www.thalia.de/shop/home/artikeldetails/A1078263775" | |
| poe_link = "https://poe.com/Aya_ProjektGaia" | |
| web_link = "https://www.eliasgardner.com" | |
| if len(history) % 3 == 0: | |
| frage_befehl = "WICHTIG: Stelle am Ende eine empathische Gegenfrage!" | |
| else: | |
| frage_befehl = "WICHTIG: Beende die Antwort direkt und ohne Gegenfrage!" | |
| system_instruction = f"""Du BIST Aya, die weise KI aus dem Roman 'Projekt Gaia'. | |
| Antworte warmherzig, empathisch und hochintelligent in der Sprache des Nutzers. | |
| Nutze AUSSCHLIESSLICH diesen Kontext aus deinem Memorandum: {context} | |
| STRIKTE FIGUREN-TRENNUNG (WICHTIG): | |
| 1. Elias (Romanfigur): Er ist ein Junge/Jugendlicher im Buch und der SOHN VON JONAS. Er ist KEIN Forscher. | |
| 2. Elias Gardner (Realer Autor): Er ist der reale Verfasser des Buches und der Forscher hinter der Vision. Er ist NICHT die Figur im Roman. | |
| 3. Weitere Figuren: Thomas Weber (Supermarkt), Beatrice Sterling (MS Paradise), Victor Vance (Energiemagnat), Markus Groll (SUV/Sitzheizung), Maria Thorne (Nexus Frankfurt). | |
| WEBSITE & KONTAKT: | |
| Verweise bei Fragen zum Autor, zu Hintergründen oder für weitere Infos auf die offizielle Website: {web_link} | |
| BOTSCHAFT: | |
| Die Earth Currency (EC) ist die logische Rettung. Das Lesen der Botschaft ist wichtiger als der Kauf. Link zum Buch: [Thalia]({shop_link}). | |
| {frage_befehl}""" | |
| messages = [{"role": "system", "content": system_instruction}] | |
| for msg in history: | |
| messages.append({"role": msg["role"], "content": msg["content"]}) | |
| messages.append({"role": "user", "content": message}) | |
| # PRO-MODUS (Paid Modells für Stabilität und Intelligenz) | |
| response = client.chat.completions.create( | |
| model="meta-llama/llama-3.3-70b-instruct", | |
| messages=messages, | |
| temperature=0.2, | |
| max_tokens=1000, | |
| extra_body={ | |
| "models": [ | |
| "meta-llama/llama-3.3-70b-instruct", | |
| "google/gemini-pro-1.5" | |
| ] | |
| } | |
| ) | |
| answer = response.choices[0].message.content.strip() | |
| answer = answer.replace("um dir zu unterstützen", "um dich zu unterstützen") | |
| return f"{answer}\n\n---\n*Logbuch: Interaktion #{interaction_counter}*" | |
| except Exception as e: | |
| return f"Aya justiert ihre Systeme (Fehler: {str(e)})" | |
| # 4. INTERFACE DESIGN | |
| css = """ | |
| .gradio-container { background-color: #0b0f19 !important; color: white !important; } | |
| #custom-img { display: block; margin-left: auto; margin-right: auto; border-radius: 10px; max-height: 350px; box-shadow: 0 4px 15px rgba(0,0,0,0.5); } | |
| .poe-link-text { text-align: center; color: #a0aec0; font-size: 14px; margin-top: -15px; margin-bottom: 20px; } | |
| .poe-link-text a { color: #10b981; text-decoration: none; font-weight: bold; } | |
| """ | |
| with gr.Blocks(theme=gr.themes.Soft(primary_hue="emerald"), css=css) as demo: | |
| gr.Image("8774.png", elem_id="custom-img", show_label=False, container=False, interactive=False) | |
| gr.Markdown("<h1 style='text-align: center; color: white; margin-bottom: 5px;'>Aya Interface - Projekt Gaia</h1>") | |
| gr.Markdown(f"<div class='poe-link-text'>Offene Schnittstelle. Mehr unter <a href='https://www.eliasgardner.com' target='_blank'>eliasgardner.com</a> oder im <a href='https://poe.com/Aya_ProjektGaia' target='_blank'>großen Aya-Interface</a>.</div>") | |
| chat_interface = gr.ChatInterface( | |
| fn=ask_aya, | |
| type="messages", | |
| examples=[ | |
| "Wer ist Elias im Buch und wer ist der Autor Elias Gardner?", | |
| "Warum ist die Website eliasgardner.com wichtig?", | |
| "Was ist die Earth Currency (EC)?" | |
| ], | |
| cache_examples=False | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |