Update app.py
Browse files1 unmodified line2345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182831 unmodified linefrom huggingface_hub import InferenceClientimport time# 1. UNGANISHA NA MODEL (UBONGO)# Tunatumia model yako ya Gemma 4B kupitia Inference APIclient = InferenceClient("google/gemma-2-2b-it") # Unaweza kubadili na repo yako ya Leno-V1 hapa# 2. CSS (Muonekano wa Mr AI)custom_css = """#chatbot { height: 70vh !important; border-radius: 15px; }.gradio-container { font-family: 'Inter', sans-serif; }#input-box textarea { border-radius: 10px !important; border: 1px solid #ddd; }"""def generate_response(message, history):user_text = message["text"]# Tunatengeneza mfumo wa mazungumzo (Prompt Engineering)messages = [{"role": "system", "content": "Wewe ni Leno-V1-Omni, AI mtaalamu uliyetengenezwa na Mr AI (Lenovatus). Jibu kwa Kiswahili fasaha au Kiingereza kulingana na user."}]# Ongeza historia ya nyuma ili AI ikumbuke mlichoongeafor msg in history:messages.append(msg)# Ongeza swali jipya la usermessages.append({"role": "user", "content": user_text})# Ongeza ujumbe wa user kwenye UIhistory.append({"role": "user", "content": user_text})# Andaa nafasi ya jibu la AIhistory.append({"role": "assistant", "content": ""})# Piga simu kwa "Ubongo" wa AI (Inference API)response_stream = client.chat_completion(messages=messages,max_tokens=1000,stream=True,temperature=0.7)for chunk in response_stream:content = chunk.choices[0].delta.contentif content:history[-1]["content"] += contentyield history, gr.MultimodalTextbox(interactive=False, placeholder="Leno AI anafikiri...", lines=1)with gr.Blocks(title="Leno AI - Workspace") as lenocore_app:with gr.Row():with gr.Column(scale=1, min_width=250):gr.Markdown("### π LenoCore Workspace")gr.Dropdown(choices=["General Chat"], value="General Chat", label="Session")gr.HTML("<hr>")gr.Markdown("**Engine Info**\n* Type: Inference API\n* Status: Active")with gr.Column(scale=4):gr.Markdown("## π Leno-V1-Omni (Powered by Mr AI)")chat_window = gr.Chatbot(elem_id="chatbot", type="messages")user_input = gr.MultimodalTextbox(interactive=True,placeholder="Uliza chochote kwa Leno AI...",show_label=False,elem_id="input-box",lines=1,max_lines=10)user_input.submit(generate_response,inputs=[user_input, chat_window],outputs=[chat_window, user_input]).then(lambda: gr.MultimodalTextbox(interactive=True, placeholder="Uliza chochote...", lines=1),None,[user_input])if __name__ == "__main__":lenocore_app.launch(css=custom_css, theme=gr.themes.Soft())1 unmodified line234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041 unmodified linefrom huggingface_hub import InferenceClientimport time# 1. UNGANISHA NA MODEL YAKO (Leno-V1-Omni)# Hapa ndipo tulipoandika jina la akaunti yako (Lenocore) na model yakoMODEL_ID = "Lenocore/Leno-V1-Omni"client = InferenceClient(MODEL_ID)# 2. CSS kwa ajili ya muonekano wa kisasa wa Mr AI Workspacecustom_css = """#chatbot { height: 70vh !important; border-radius: 15px; box-shadow: 0 4px 6px rgba(0,0,0,0.1); }.gradio-container { font-family: 'Inter', sans-serif; }#input-box textarea { border-radius: 10px !important; transition: all 0.2s ease; border: 1px solid #ddd; }#input-box textarea:focus { border-color: #7b1fa2 !important; box-shadow: 0 0 5px rgba(123, 31, 162, 0.2); }"""def generate_response(message, history):# message inakuja kama dict: {"text": "...", "files": [...]}user_text = message["text"]# Maelekezo ya msingi ya AI (System Prompt)messages = [{"role": "system", "content": "Wewe ni Leno-V1-Omni, AI msaidizi mtaalamu uliyetengenezwa na Lenovatus (Mr AI). Jibu kwa ustadi na heshima."}]# Ongeza historia ya mazungumzo (Gradio 6 inatumia list of dicts)for msg in history:messages.append(msg)# Ongeza swali jipya la usermessages.append({"role": "user", "content": user_text})# Andaa muundo wa kuonyesha kwenye chat windowhistory.append({"role": "user", "content": user_text})history.append({"role": "assistant", "content": ""})try:# Inaita model yako kule Hugging Faceresponse_stream = client.chat_completion(messages=messages,max_tokens=1000,stream=True,temperature=0.7)for chunk in response_stream:content = chunk.choices[0].delta.contentif content:history[-1]["content"] += content# 'yield' inafanya jibu lijitokeze herufi kwa herufi (streaming)yield history, gr.MultimodalTextbox(interactive=False, placeholder="Leno-V1 anafikiria...", lines=1)except Exception as e:history[-1]["content"] = f"Hitilafu: Hakikisha 'Inference API' imewashwa kwenye Settings za model yako ({MODEL_ID}).\nMaelezo: {str(e)}"yield history, gr.MultimodalTextbox(interactive=True)# 3. JENGA INTERFACE (UI)with gr.Blocks(title="Leno AI - Workspace") as lenocore_app:with gr.Row():# SIDEBAR (π Dashboard)with gr.Column(scale=1, min_width=250):gr.Markdown(f"### π LenoCore\n**Model:** {MODEL_ID}")gr.Dropdown(choices=["General Chat", "Dev Session"], value="General Chat", label="Current Session")gr.HTML("<hr style='border: 0; border-top: 1px solid #eee; margin: 20px 0;'>")gr.Markdown("**System Status**\n* Engine: Adaptive\n* API: Connected")# MAIN CHAT AREAwith gr.Column(scale=4):gr.Markdown("## π Leno-V1-Omni Pro Workspace")# Chatbot bila vigezo vinavyoleta error (type/copy_button)chat_window = gr.Chatbot(elem_id="chatbot")with gr.Row():# Textbox inayotanuka (expandable)user_input = gr.MultimodalTextbox(interactive=True,placeholder="Andika ujumbe au swali kwa Leno-V1...",show_label=False,scale=8,elem_id="input-box",lines=1,max_lines=12)# 4. TRIGGERS (Matukio)user_input.submit(generate_response,inputs=[user_input, chat_window],outputs=[chat_window, user_input]).then(# Inarudisha textbox kuwa ndogo (lines=1) baada ya kutumalambda: gr.MultimodalTextbox(interactive=True, placeholder="Andika ujumbe...", lines=1),None,[user_input])# 5. WASHA APPif __name__ == "__main__":# CSS na Theme vinawekwa hapa kwa ajili ya Gradio 6+lenocore_app.launch(css=custom_css, theme=gr.themes.Soft())
app.py+60-39