Spaces:
				
			
			
	
			
			
		Runtime error
		
	
	
	
			
			
	
	
	
	
		
		
		Runtime error
		
	Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | @@ -6,14 +6,14 @@ MODEL = "prithivMLmods/Llama-SmolTalk-3.2-1B-Instruct" | |
| 6 | 
             
            # Charger le tokenizer
         | 
| 7 | 
             
            tokenizer = AutoTokenizer.from_pretrained(MODEL)
         | 
| 8 |  | 
| 9 | 
            -
            # Charger le modèle en 8 bits | 
| 10 | 
             
            model = AutoModelForCausalLM.from_pretrained(
         | 
| 11 | 
             
                MODEL,
         | 
| 12 | 
             
                device_map="auto",
         | 
| 13 | 
            -
                load_in_8bit=True | 
| 14 | 
             
            )
         | 
| 15 |  | 
| 16 | 
            -
            # Pipeline | 
| 17 | 
             
            chatbot = pipeline(
         | 
| 18 | 
             
                "text-generation",
         | 
| 19 | 
             
                model=model,
         | 
| @@ -22,34 +22,40 @@ chatbot = pipeline( | |
| 22 | 
             
            )
         | 
| 23 |  | 
| 24 | 
             
            # Prompt système
         | 
| 25 | 
            -
            system_prompt =  | 
|  | |
|  | |
|  | |
|  | |
|  | |
| 26 |  | 
| 27 | 
            -
            def chat(message, history | 
| 28 | 
            -
                 | 
| 29 | 
            -
                 | 
| 30 | 
            -
                
         | 
| 31 | 
            -
                context = "\n".join([f"Utilisateur: {m[0]}\nAria: {m[1]}" for m in history])
         | 
| 32 | 
            -
                prompt = f"{system_prompt}\n{context}\nUtilisateur: {message}\nAria:"
         | 
| 33 |  | 
| 34 | 
             
                resp = chatbot(
         | 
| 35 | 
             
                    prompt,
         | 
| 36 | 
            -
                    max_new_tokens= | 
| 37 | 
             
                    do_sample=True,
         | 
| 38 | 
             
                    temperature=0.7,
         | 
| 39 | 
             
                    top_p=0.9,
         | 
| 40 | 
             
                    repetition_penalty=1.1
         | 
| 41 | 
             
                )[0]["generated_text"]
         | 
| 42 | 
            -
             | 
| 43 | 
            -
                 | 
| 44 | 
            -
                 | 
| 45 | 
            -
                
         | 
| 46 | 
            -
             | 
| 47 | 
            -
                 | 
| 48 | 
            -
                return  | 
| 49 |  | 
| 50 | 
             
            with gr.Blocks() as demo:
         | 
| 51 | 
            -
                 | 
|  | |
| 52 | 
             
                msg = gr.Textbox(placeholder="Écris un message...")
         | 
| 53 | 
            -
             | 
|  | |
|  | |
| 54 |  | 
| 55 | 
             
            demo.launch()
         | 
|  | |
| 6 | 
             
            # Charger le tokenizer
         | 
| 7 | 
             
            tokenizer = AutoTokenizer.from_pretrained(MODEL)
         | 
| 8 |  | 
| 9 | 
            +
            # Charger le modèle en 8 bits
         | 
| 10 | 
             
            model = AutoModelForCausalLM.from_pretrained(
         | 
| 11 | 
             
                MODEL,
         | 
| 12 | 
             
                device_map="auto",
         | 
| 13 | 
            +
                load_in_8bit=True
         | 
| 14 | 
             
            )
         | 
| 15 |  | 
| 16 | 
            +
            # Pipeline
         | 
| 17 | 
             
            chatbot = pipeline(
         | 
| 18 | 
             
                "text-generation",
         | 
| 19 | 
             
                model=model,
         | 
|  | |
| 22 | 
             
            )
         | 
| 23 |  | 
| 24 | 
             
            # Prompt système
         | 
| 25 | 
            +
            system_prompt = (
         | 
| 26 | 
            +
                "Tu es Aria, une IA gentille, claire et polie. "
         | 
| 27 | 
            +
                "Réponds toujours en phrases complètes. "
         | 
| 28 | 
            +
                "Ne te lances pas dans un jeu de rôle, ne répète pas les messages précédents, "
         | 
| 29 | 
            +
                "et donne uniquement ta réponse."
         | 
| 30 | 
            +
            )
         | 
| 31 |  | 
| 32 | 
            +
            def chat(message, history):
         | 
| 33 | 
            +
                history = history or []
         | 
| 34 | 
            +
                context = "\n".join([f"{user}\n{bot}" for user, bot in history[-3:]])
         | 
| 35 | 
            +
                prompt = f"{system_prompt}\n{context}\n{message}\nRéponse:"
         | 
|  | |
|  | |
| 36 |  | 
| 37 | 
             
                resp = chatbot(
         | 
| 38 | 
             
                    prompt,
         | 
| 39 | 
            +
                    max_new_tokens=250,   # plus long pour éviter les coupures
         | 
| 40 | 
             
                    do_sample=True,
         | 
| 41 | 
             
                    temperature=0.7,
         | 
| 42 | 
             
                    top_p=0.9,
         | 
| 43 | 
             
                    repetition_penalty=1.1
         | 
| 44 | 
             
                )[0]["generated_text"]
         | 
| 45 | 
            +
             | 
| 46 | 
            +
                # Couper dès qu'il repart sur un nouveau tour
         | 
| 47 | 
            +
                reply = resp.split("Réponse:")[-1].strip()
         | 
| 48 | 
            +
                reply = reply.split("Utilisateur:")[0].strip()
         | 
| 49 | 
            +
             | 
| 50 | 
            +
                history.append((message, reply))
         | 
| 51 | 
            +
                return history, history
         | 
| 52 |  | 
| 53 | 
             
            with gr.Blocks() as demo:
         | 
| 54 | 
            +
                chatbot_ui = gr.Chatbot()
         | 
| 55 | 
            +
                state = gr.State([])  # sauvegarde de l'historique
         | 
| 56 | 
             
                msg = gr.Textbox(placeholder="Écris un message...")
         | 
| 57 | 
            +
             | 
| 58 | 
            +
                msg.submit(chat, [msg, state], [chatbot_ui, state])
         | 
| 59 | 
            +
                msg.submit(lambda: "", None, msg)  # reset input après envoi
         | 
| 60 |  | 
| 61 | 
             
            demo.launch()
         | 
