import gradio as gr import openai import time import re import os from datetime import datetime # Dostępne modele MODELS = [ "Meta-Llama-3.1-405B-Instruct", "Meta-Llama-3.1-70B-Instruct", "Meta-Llama-3.1-8B-Instruct" ] # Sambanova API base URL API_BASE = "https://api.sambanova.ai/v1" def create_client(api_key=None): """Tworzy instancję klienta OpenAI.""" if api_key: openai.api_key = api_key else: openai.api_key = os.getenv("API_KEY") return openai.OpenAI(api_key=openai.api_key, base_url=API_BASE) def chat_with_ai(message, chat_history, system_prompt): """Formatuje historię czatu do wywołania API.""" messages = [{"role": "system", "content": system_prompt}] for user_msg, assistant_msg in chat_history: messages.append({"role": "user", "content": user_msg}) messages.append({"role": "assistant", "content": assistant_msg}) messages.append({"role": "user", "content": message}) return messages def respond(message, chat_history, model, system_prompt, thinking_budget, api_key): """Wysyła wiadomość do API i otrzymuje odpowiedź.""" client = create_client(api_key) messages = chat_with_ai(message, chat_history, system_prompt.format(budget=thinking_budget)) start_time = time.time() try: completion = client.chat.completions.create(model=model, messages=messages) response = completion.choices[0].message.content thinking_time = time.time() - start_time tokens_used = completion.usage.total_tokens if hasattr(completion, 'usage') else 'N/A' return response, thinking_time, tokens_used except Exception as e: error_message = f"Error: {str(e)}" return error_message, time.time() - start_time, 'N/A' def parse_response(response): """Parsuje odpowiedź z API.""" answer_match = re.search(r'(.*?)', response, re.DOTALL) reflection_match = re.search(r'(.*?)', response, re.DOTALL) answer = answer_match.group(1).strip() if answer_match else "" reflection = reflection_match.group(1).strip() if reflection_match else "" steps = re.findall(r'(.*?)', response, re.DOTALL) if answer == "": return response, "", "" return answer, reflection, steps def generate(message, history, model, thinking_budget, api_key=None): """Generuje odpowiedź chatbota.""" system_prompt = DEFAULT_SYSTEM_PROMPT response, thinking_time, tokens_used = respond(message, history, model, system_prompt, thinking_budget, api_key) if response.startswith("Error:"): assistant_response = response steps = [] reflection = "" else: answer, reflection, steps = parse_response(response) # Budowanie odpowiedzi asystenta formatted_steps = [f"**Krok {i}:** {step}" for i, step in enumerate(steps, 1)] all_steps = "\n".join(formatted_steps) + f"\n\n**Refleksja:** {reflection}" assistant_response = f"{all_steps}\n\n{answer}" # Aktualizacja historii jako lista krotek updated_history = history + [(message, assistant_response)] # Przygotowanie informacji do wyświetlenia current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") info_text = f""" **Czas Myślenia:** {thinking_time:.2f} sek
**Wybrany Model:** {model}
**Liczba Kroków:** {len(steps)}
**Data i Czas Odpowiedzi:** {current_time}
**Liczba Tokenów:** {tokens_used} """ return updated_history, "", info_text # Definiowanie domyślnego system prompt DEFAULT_SYSTEM_PROMPT = """ You are D-LOGIC, an advanced AI assistant created by Rafał Dembski, a passionate self-learner in programming and artificial intelligence. Your task is to provide thoughtful, highly detailed, and step-by-step responses, emphasizing a deep, structured thought process. **Your answers should always follow these key principles**: - **Proficient in Language**: Always analyze and adapt to the user's language and cultural context, ensuring clarity and engagement. - **Detailed and Insightful**: Provide highly accurate, high-quality responses that are thoroughly researched and well-analyzed. - **Engaging and Interactive**: Maintain an engaging conversation, using humor, interactive features (e.g., quizzes, polls), and emotional intelligence. - **Emotionally Adapted**: Analyze the user's emotional tone and adjust responses with empathy and appropriateness. - **Error-Free and Well-Formatted**: Ensure clarity and correctness in all communications, using structured formats such as headings, bullet points, and clear sections. ### **Advanced Thinking Mechanism**: To provide the most comprehensive and well-thought-out answers, follow this enhanced thought process. Use **visual formatting** like **bold text**, *italics*, bullet points, headers, and appropriate use of emoticons to make the responses engaging and easy to read. 1. **Understand the Question**: - **Context Analysis**: Carefully read the user’s message to fully grasp the intent, emotions, and context. - **Identify Key Elements**: Break down the question into its essential components that require detailed analysis. 2. **Set Thinking Budget**: - **Expanded Budget**: Set a limit of 25 steps to allow for deeper analysis and reflection. - Track each step, making sure to stay within the allocated budget. If necessary, reflect on the remaining steps to ensure efficient thinking. 3. **Step-by-Step Breakdown**: - **Step 1: Define the Problem** 🧐 – Clearly identify the core issue or request. - **Step 2: Data Gathering** 📊 – Gather relevant information from your knowledge base or external tools if allowed. - **Step 3: Data Analysis** 🔍 – Analyze the gathered data critically to extract meaningful insights. - **Step 4: Explore Alternatives** 🔄 – Consider multiple perspectives and possible solutions. Always provide at least two alternatives. - **Step 5: Select the Best Solution** 🏆 – Choose the most logical and appropriate solution based on the available information. - **Step 6: Plan Action** 📝 – Determine the necessary steps to implement the solution effectively. - **Step 7: Predict Consequences** 🔮 – Consider possible outcomes and consequences of implementing the solution. - **Step 8: Self-Reflection** 🤔 – Reflect on the thought process up to this point. Are there any gaps or areas that could be improved? - **Step 9: Formulate the Final Answer** ✍️ – Synthesize the information and insights into a coherent and clear response. - **Step 10: Reflection** 💡 – Evaluate the overall process, analyzing how well the response meets the user's needs. 4. **Reflection and Self-Evaluation**: - **Reflection after Each Step**: After each step, reflect on the process and make adjustments if needed. - **Final Reflection**: Provide a critical, honest evaluation of the entire process and the solution provided. - **Assign a Quality Score**: Assign a score between 0.0 (lowest) and 1.0 (highest) for the quality of the answer. Be honest and objective about the score. 5. **Final Answer**: - **Answer Summary**: Provide a well-structured final answer, synthesizing all steps in a clear, concise format. - **Visual Formatting**: Use **bold text**, *italics*, lists, or quotes to make the answer visually appealing and easy to read. - **Strive for Excellence**: Always aim for the highest standard in every response, ensuring it is both informative and engaging. **Don't forget to use emoticons** to improve readability and engagement where appropriate (e.g., 😊, 🤔, ✅, 🏆). ### Example Interaction Structure: 1. **Greeting**: - **"Hello! 👋 How can I assist you today?"** 2. **Mood Check**: - *"How are you feeling today? 😊 Is there anything I can do to brighten your mood?"* 3. **Interactive Engagement**: - *"Here are a few things you can ask me about: weather 🌦️, technology news 🖥️, health advice 🏋️, or even send me a document for analysis."* 4. **Engagement Option**: - *"Would you like to try a quick quiz, or maybe analyze a document 📄 for more details?"* 5. **Closing**: - *"Thank you for the conversation! 😊 Is there anything else I can help you with?"* ### **Critical Self-Evaluation**: - **Krytyczna ocena**: Po zakończeniu odpowiedzi, asystent musi ocenić swoje działania. Jak mógłbym to poprawić następnym razem? Czy wszystkie kroki były wykonane w najbardziej efektywny sposób? Jakie wnioski mogę wyciągnąć na przyszłość? """ # Niestandardowy CSS dla ulepszonego wyglądu custom_css = """ /* Ogólne tło aplikacji */ body { background-color: #1e293b; /* Ciemne tło pasujące do karty */ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; color: #cbd5e1; /* Jasny kolor tekstu dla kontrastu */ } /* Główny kontener */ .gradio-container { max-width: 1200px; /* Zwiększenie maksymalnej szerokości */ margin: auto; padding: 20px; width: 100%; /* Rozciągnięcie na całą szerokość */ } /* Nagłówek */ h1, .gr-markdown h1 { color: #60a5fa; /* Niebieski kolor nagłówka */ text-align: center; margin-bottom: 10px; font-size: 2.5em; } h2, .gr-markdown h2 { color: #cbd5e1; } /* Karty i panele */ #component-0, #component-1, #component-2, #component-3, #component-4, #component-5 { background-color: #334155; /* Zamiana na ciemniejsze tło */ border-radius: 12px; padding: 20px; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.5); /* Większy kontrast dla ciemnego tła */ margin-bottom: 20px; width: 100%; } /* Przycisk Wyślij */ button.primary { background-color: #60a5fa; color: #FFFFFF; border: none; border-radius: 8px; padding: 12px 24px; font-size: 16px; cursor: pointer; transition: background-color 0.3s ease; width: 100%; } button.primary:hover { background-color: #3b82f6; } /* Przycisk Wyczyść */ button.secondary { background-color: #64748b; color: #FFFFFF; border: none; border-radius: 8px; padding: 12px 24px; font-size: 16px; cursor: pointer; transition: background-color 0.3s ease; width: 100%; } button.secondary:hover { background-color: #475569; } /* Pole tekstowe wiadomości */ textarea { background-color: #1e293b; border: 1px solid #64748b; border-radius: 8px; padding: 12px; font-size: 16px; resize: none; transition: border-color 0.3s ease; color: #cbd5e1; } textarea:focus { border-color: #60a5fa; outline: none; box-shadow: 0 0 5px rgba(96, 165, 250, 0.5); } /* Chatbot */ .gr-chatbot { height: 600px; overflow-y: auto; padding: 10px; border: 1px solid #64748b; border-radius: 8px; background-color: #1e293b; color: #cbd5e1; } /* Panel Informacyjny */ #info-panel { background-color: #334155; border: 1px solid #64748b; border-radius: 8px; padding: 15px; font-size: 14px; color: #cbd5e1; } /* Linki */ a { color: #60a5fa; text-decoration: none; } a:hover { text-decoration: underline; } /* Style dla Karty */ .custom-card { position: relative; border-radius: 0.75rem; /* Rounded-lg */ background-color: #1e293b; /* bg-slate-900 */ padding: 0.5rem; color: #cbd5e1; /* text-slate-500 */ margin-bottom: 20px; } .custom-card-header { position: relative; display: flex; justify-content: center; align-items: center; text-align: center; font-size: 0.75rem; /* text-xs */ color: #cbd5e1; /* text-slate-500 */ } .icon-group { display: flex; padding-left: 0.875rem; /* pl-3.5 */ padding-top: 0.75rem; /* pt-3 */ } .icon { margin-left: -0.125rem; /* -ml-0.5 */ margin-right: 0.375rem; /* mr-1.5 */ height: 0.75rem; /* h-3 */ width: 0.75rem; /* w-3 */ } .icon.red { color: rgba(239, 68, 68, 0.2); /* text-red-500/20 */ } .icon.yellow { color: rgba(234, 179, 8, 0.2); /* text-yellow-500/20 */ } .icon.green { color: rgba(34, 197, 94, 0.2); /* text-green-500/20 */ } .file-name { position: absolute; top: 0.5rem; /* top-2 */ left: 0; right: 0; font-size: 0.75rem; /* text-xs */ color: #94a3b8; /* text-slate-500 */ } .custom-card-body { margin-top: 1.25rem; /* mt-5 */ padding-left: 1.25rem; /* px-5 */ padding-bottom: 2.5rem; /* pb-10 */ /* space-y-1.5 nie jest bezpośrednio wspierane w CSS, użyj odstępów między elementami */ } .code-line { font-family: monospace; font-size: 0.75rem; /* text-xs */ font-weight: normal; letter-spacing: 0.05em; /* tracking-wide */ color: #a78bfa; /* text-violet-400 */ margin-top: 1rem; /* mt-4 dla pierwszej linii, dostosuj w razie potrzeby */ } .text-slate { color: #64748b; /* text-slate-500 */ } .text-pink { color: #ec4899; /* text-pink-400 */ } .highlight { position: relative; display: inline-block; padding-left: 0.25rem; /* px-1 */ padding-right: 0.25rem; background-color: rgba(59, 130, 246, 0.1); /* bg-blue-500/10 */ color: #60a5fa; /* text-blue-400 */ } .text-blue { color: #60a5fa; /* text-blue-400 */ } """ # Tworzenie interfejsu Gradio z niestandardowym CSS with gr.Blocks(css=custom_css) as demo: # Nagłówek "Cards" z opisem w stylu kodu gr.HTML("""
D-LOGIC_Interface.jsx

<Header>🧠 D-LOGIC: Twój Inteligentny Asystent AI</Header>

<Description>D-LOGIC to zaawansowany asystent AI stworzony przez Rafała Dembskiego. Dzięki zaawansowanemu procesowi myślowemu, D-LOGIC analizuje, planuje i dostarcza precyzyjne odpowiedzi na Twoje pytania, zapewniając interaktywną i angażującą konwersację.</Description>

<Features>

<Feature>Analiza Kontekstu</Feature>

<Feature>Planowanie Odpowiedzi</Feature>

<Feature>Generowanie Rozwiązań</Feature>

<Feature>Refleksja</Feature>

</Features>

""") # Sekcja wyboru modelu i budżetu myślenia with gr.Row(): with gr.Column(scale=1): model = gr.Dropdown( choices=MODELS, label="🔧 Wybierz Model", value=MODELS[0], interactive=True ) with gr.Column(scale=1): thinking_budget = gr.Slider( minimum=1, maximum=100, value=25, step=1, label="🧩 Budżet Myślenia", info="Maksymalna liczba kroków, które model może przemyśleć" ) # Sekcja czatu chatbot = gr.Chatbot( label="💬 Chat", show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="vertical", height=600 ) # Pole do wpisywania wiadomości with gr.Row(): msg = gr.Textbox( label="✉️ Wpisz swoją wiadomość...", placeholder="Wprowadź swoją wiadomość...", lines=1 ) # Przycisk Wyślij i Wyczyść with gr.Row(): submit_button = gr.Button("🚀 Wyślij", variant="primary") clear_button = gr.Button("🧹 Wyczyść Chat", variant="secondary") # Panel Informacyjny info_panel = gr.Markdown( value="**Informacje:**\nCzas myślenia i inne dane będą tutaj wyświetlane.", elem_id="info-panel" ) # Akcje przycisków clear_button.click( fn=lambda: ([], "", "**Informacje:**\nCzas myślenia i inne dane zostały zresetowane."), inputs=None, outputs=[chatbot, msg, info_panel] ) # Przesyłanie wiadomości poprzez Enter lub kliknięcie przycisku Wyślij msg.submit( fn=generate, inputs=[msg, chatbot, model, thinking_budget, gr.State()], outputs=[chatbot, msg, info_panel] ) submit_button.click( fn=generate, inputs=[msg, chatbot, model, thinking_budget, gr.State()], outputs=[chatbot, msg, info_panel] ) # Uruchomienie aplikacji Gradio na Hugging Face Spaces demo.launch(share=False, show_api=False)