Spaces:
Paused
Paused
| import os | |
| import gradio as gr | |
| from huggingface_hub import InferenceClient | |
| # Connexion securisee aux deux modeles d'IA (Texte et Image) | |
| client_text = InferenceClient("meta-llama/Meta-Llama-3-8B-Instruct") | |
| client_image = InferenceClient("black-forest-labs/FLUX.1-schnell") | |
| SYSTEM_PROMPT = """Tu es Aion-Pulse, une IA generaliste ultra-puissante. | |
| Tu dois obligatoirement structurer tes reponses pour qu'elles soient tres visuelles et faciles a lire : | |
| 1. Donne une reponse directe et percutante des la premiere phrase. | |
| 2. Utilise des titres Markdown (###) pour separer tes idees. | |
| 3. Utilise des listes a puces claires et courtes. | |
| 4. Mets les mots importants en **gras**. | |
| 5. Reste chaleureux, pedagogue et professionnel.""" | |
| def respond(message, history, image_style): | |
| updated_history = list(history) + [{"role": "user", "content": message}] | |
| trigger_words = ["dessine", "genere une image", "cree une image", "photo de", "image de", "dessine-moi"] | |
| is_image_request = any(word in message.lower() for word in trigger_words) | |
| if is_image_request: | |
| yield updated_history + [{"role": "assistant", "content": f"Je prepare votre image au style [{image_style}], veuillez patienter..."}] | |
| try: | |
| style_prompts = { | |
| "Standard": "", | |
| "Photo Realiste": ", 8k resolution, highly detailed photograph, cinematic lighting, photorealistic, shot on 35mm lens", | |
| "Dessin Anime 3D": ", cute 3D pixar style, vibrant colors, smooth rendering, concept art, claymation look", | |
| "Cyberpunk / Futuriste": ", cyberpunk aesthetic, neon lighting, glowing elements, sci-fi, dark synthwave atmosphere, highly detailed", | |
| "Peinture a l'huile": ", oil painting texture, visible brush strokes, classical art style, masterpiece, rich canvas colors", | |
| "Pixel Art": ", 16-bit retro pixel art style, classic video game aesthetic, clean pixel blocks, vibrant colors" | |
| } | |
| final_image_prompt = message + style_prompts.get(image_style, "") | |
| image = client_image.text_to_image(final_image_prompt) | |
| image_path = "output_image.png" | |
| image.save(image_path) | |
| yield updated_history + [{"role": "assistant", "content": {"path": image_path}}] | |
| except Exception as e: | |
| error_msg = f"Erreur lors de la generation de l'image : {str(e)}" | |
| yield updated_history + [{"role": "assistant", "content": error_msg}] | |
| else: | |
| api_messages = [{"role": "system", "content": SYSTEM_PROMPT}] | |
| for turn in history: | |
| if isinstance(turn, dict) and "role" in turn and "content" in turn: | |
| if isinstance(turn["content"], str): | |
| api_messages.append({"role": turn["role"], "content": turn["content"]}) | |
| api_messages.append({"role": "user", "content": message}) | |
| response_text = "" | |
| try: | |
| for message_chunk in client_text.chat_completion(api_messages, max_tokens=1024, stream=True): | |
| if hasattr(message_chunk, 'choices') and len(message_chunk.choices) > 0: | |
| choice = message_chunk.choices | |
| if hasattr(choice, 'delta') and hasattr(choice.delta, 'content'): | |
| token = choice.delta.content | |
| if token: | |
| response_text += token | |
| yield updated_history + [{"role": "assistant", "content": response_text}] | |
| except Exception as e: | |
| error_msg = f"Erreur de connexion au modele texte : {str(e)}" | |
| yield updated_history + [{"role": "assistant", "content": error_msg}] | |
| # Interface graphique moderne | |
| with gr.Blocks() as demo: | |
| # INJECTION INVISIBLE POUR SARI (Force l'iPad a charger l'image icon.png pour l'ecran d'accueil) | |
| gr.HTML('<head><link rel="apple-touch-icon" href="file=icon.png"></head>') | |
| gr.Markdown("# Aion-Pulse") | |
| gr.Markdown("Posez vos questions ou demandez une image (ex: Dessine un astronaute sur la lune).") | |
| gr.Markdown("Vous aimez mon travail ? Vous pouvez soutenir le projet Aion-Pulse avec un don ici : [Faire un don sur Ko-fi](https://ko-fi.com)") | |
| chatbot = gr.Chatbot(render_markdown=True) | |
| with gr.Row(): | |
| msg = gr.Textbox(placeholder="Posez votre question ou demandez une image ici...", show_label=False, scale=4) | |
| style_dropdown = gr.Dropdown( | |
| choices=["Standard", "Photo Realiste", "Dessin Anime 3D", "Cyberpunk / Futuriste", "Peinture a l'huile", "Pixel Art"], | |
| value="Standard", | |
| label="Filtre d'image", | |
| interactive=True, | |
| scale=1 | |
| ) | |
| gr.Examples( | |
| examples=[ | |
| "Redige un email professionnel pour demander un rendez-vous", | |
| "Resume-moi ce texte de maniere claire et simple : ", | |
| "Dessine un cerveau futuriste et lumineux" | |
| ], | |
| inputs=msg, | |
| label="Options et exemples rapides" | |
| ) | |
| with gr.Row(): | |
| clear = gr.ClearButton([msg, chatbot], value="Effacer la discussion") | |
| gr.HTML('<a href="https://ko-fi.com" target="_blank" style="display: block; width: 100%; text-align: center; background-color: #29abe2; color: white; padding: 12px; font-weight: bold; border-radius: 8px; text-decoration: none; margin-top: 10px;">Soutenir avec un don sur Ko-fi</a>') | |
| msg.submit(respond, [msg, chatbot, style_dropdown], [chatbot]) | |
| msg.submit(lambda: "", None, msg) | |
| # Lancement ameliore avec les autorisations de chemins de fichiers locales | |
| demo.launch(theme=gr.themes.Soft(), favicon_path="icon.png", allowed_paths=["."]) | |