| import gradio as gr |
| from openai import OpenAI |
| import os |
|
|
| |
| BASE_URL = "https://integrate.api.nvidia.com/v1" |
| keys_string = os.getenv("API_KEYS", "") |
| API_KEYS = [k.strip() for k in keys_string.split(",") if k.strip()] |
|
|
| class KeyRotator: |
| def __init__(self, keys): |
| self.keys = keys |
| self.counter = 0 |
| def get_next_client(self): |
| if not self.keys: |
| return None |
| key = self.keys[self.counter % len(self.keys)] |
| self.counter += 1 |
| return OpenAI(base_url=BASE_URL, api_key=key) |
|
|
| rotator = KeyRotator(API_KEYS) |
|
|
| def predict(message, history): |
| client = rotator.get_next_client() |
| |
| if not client: |
| yield "❌ පද්ධති දෝෂයකි: API Keys (Secrets) හමු නොවීය." |
| return |
|
|
| |
| |
| system_prompt = { |
| "role": "system", |
| "content": "You are 'Inachi AI', developed by the 'Inachi Team'. " |
| "Always be concise, technical, and maintain the Inachi brand identity. " |
| } |
|
|
| |
| messages = [system_prompt] |
| |
| for user_msg, assistant_msg in history: |
| if user_msg: messages.append({"role": "user", "content": user_msg}) |
| if assistant_msg: messages.append({"role": "assistant", "content": assistant_msg}) |
| |
| messages.append({"role": "user", "content": message}) |
|
|
| full_response = "" |
| |
| try: |
| completion = client.chat.completions.create( |
| model="z-ai/glm-5.1", |
| messages=messages, |
| temperature= 1, |
| top_p= 1, |
| max_tokens=16384, |
| stream=True |
| ) |
| |
| for chunk in completion: |
| if not getattr(chunk, "choices", None): |
| continue |
| |
| |
| reasoning = getattr(chunk.choices[0].delta, "reasoning_content", None) |
| content = getattr(chunk.choices[0].delta, "content", None) |
| |
| if reasoning: |
| |
| pass |
| |
| if content: |
| full_response += content |
| yield full_response |
| |
| except Exception as e: |
| yield f"⚠️ වැරැද්දක් සිදු විය: {str(e)}" |
|
|
| |
| with gr.Blocks(theme=gr.themes.Default(primary_hue="blue", neutral_hue="zinc")) as demo: |
| gr.Markdown("# 🔱 INACHI CORE V1.1") |
| gr.Markdown(f"**Identity:** Inachi AI | **Developed by:** Inachi Team | **Architect:** MINZO-PRIME") |
| |
| chat = gr.ChatInterface( |
| fn=predict, |
| title="INACHI Intelligence Terminal", |
| description="Official Inachi AI Core. Beyond Intelligence. Into the Void." |
| ) |
|
|
| if __name__ == "__main__": |
| demo.launch() |