Update app.py
Browse files1 unmodified line23456756789910111112131314151617181920211920222324222526272829303129323331343536343536373839404142434445463738394041424344454647484950515253545556575859606152535455626364656667686970587172736074756263646566676869767778798081828384858687888990919293947695969798991001011021031041 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# 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 (Muonekano wa Mr AI)# 2. CSS kwa ajili ya muonekano wa kisasa wa Mr AI Workspacecustom_css = """#chatbot { height: 70vh !important; border-radius: 15px; }#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; border: 1px solid #ddd; }#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"] # 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."}] # 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 nyuma ili AI ikumbuke mlichoongea # Ongeza historia ya mazungumzo (Gradio 6 inatumia list of dicts) for msg in history: messages.append(msg) # Ongeza swali jipya la user messages.append({"role": "user", "content": user_text}) # Ongeza ujumbe wa user kwenye UI # Andaa muundo wa kuonyesha kwenye chat window history.append({"role": "user", "content": user_text}) # Andaa nafasi ya jibu la AI history.append({"role": "assistant", "content": ""}) try: # 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.content if content: history[-1]["content"] += content yield history, gr.MultimodalTextbox(interactive=False, placeholder="Leno AI anafikiri...", lines=1) # Inaita model yako kule Hugging Face 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.content if 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("### π 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") 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 AREA with gr.Column(scale=4): gr.Markdown("## π Leno-V1-Omni (Powered by Mr AI)") gr.Markdown("## π Leno-V1-Omni Pro Workspace") # Chatbot bila vigezo vinavyoleta error (type/copy_button) chat_window = gr.Chatbot(elem_id="chatbot", type="messages") chat_window = gr.Chatbot(elem_id="chatbot") user_input = gr.MultimodalTextbox( interactive=True, placeholder="Uliza chochote kwa Leno AI...", show_label=False, elem_id="input-box", lines=1, max_lines=10 ) 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 kutuma lambda: gr.MultimodalTextbox(interactive=True, placeholder="Uliza chochote...", lines=1), lambda: 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