Spaces:
				
			
			
	
			
			
					
		Running
		
	
	
	
			
			
	
	
	
	
		
		
					
		Running
		
	Create app.py
Browse files
    	
        app.py
    ADDED
    
    | @@ -0,0 +1,61 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            from flask import Flask, request, render_template
         | 
| 2 | 
            +
            import google.generativeai as genai
         | 
| 3 | 
            +
            import os
         | 
| 4 | 
            +
            from PIL import Image
         | 
| 5 | 
            +
            import io
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            app = Flask(__name__)
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            # Configuration de l'API Gemini
         | 
| 10 | 
            +
            token = os.environ.get("TOKEN")
         | 
| 11 | 
            +
            genai.configure(api_key=token)
         | 
| 12 | 
            +
             | 
| 13 | 
            +
            generation_config = {
         | 
| 14 | 
            +
                "temperature": 1,
         | 
| 15 | 
            +
                "top_p": 0.95,
         | 
| 16 | 
            +
                "top_k": 64,
         | 
| 17 | 
            +
                "max_output_tokens": 8192,
         | 
| 18 | 
            +
            }
         | 
| 19 | 
            +
             | 
| 20 | 
            +
            safety_settings = [
         | 
| 21 | 
            +
                {"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
         | 
| 22 | 
            +
                {"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"},
         | 
| 23 | 
            +
                {"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE"},
         | 
| 24 | 
            +
                {"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"},
         | 
| 25 | 
            +
            ]
         | 
| 26 | 
            +
             | 
| 27 | 
            +
            mm = """resous cet exercice. tu répondras en détaillant au maximum ton procédé de calcul. réponse attendue uniquement en Latex
         | 
| 28 | 
            +
             | 
| 29 | 
            +
            """
         | 
| 30 | 
            +
            model = genai.GenerativeModel(
         | 
| 31 | 
            +
                model_name="gemini-1.5-pro",
         | 
| 32 | 
            +
                generation_config=generation_config,
         | 
| 33 | 
            +
                safety_settings=safety_settings,
         | 
| 34 | 
            +
            )
         | 
| 35 | 
            +
             | 
| 36 | 
            +
             | 
| 37 | 
            +
            @app.route("/", methods=["GET", "POST"])
         | 
| 38 | 
            +
            def index():
         | 
| 39 | 
            +
                e = ""
         | 
| 40 | 
            +
                if request.method == "POST":
         | 
| 41 | 
            +
                    if "image" not in request.files:
         | 
| 42 | 
            +
                        e = "Aucune image sélectionnée."
         | 
| 43 | 
            +
                    else:
         | 
| 44 | 
            +
                        image_file = request.files["image"]
         | 
| 45 | 
            +
                        try:
         | 
| 46 | 
            +
                            image = Image.open(io.BytesIO(image_file.read()))
         | 
| 47 | 
            +
             | 
| 48 | 
            +
                            response = model.generate_content([mm, image])  # Passage de l'image
         | 
| 49 | 
            +
                            print(response.text)
         | 
| 50 | 
            +
                            e = response.text
         | 
| 51 | 
            +
             | 
| 52 | 
            +
             | 
| 53 | 
            +
                        except Exception as e: # gérer les erreurs potentielles d'ouverture de l'image
         | 
| 54 | 
            +
                              e = f"Erreur lors du traitement de l'image : {str(e)}"
         | 
| 55 | 
            +
             | 
| 56 | 
            +
             | 
| 57 | 
            +
                return render_template("index.html", e=e)
         | 
| 58 | 
            +
             | 
| 59 | 
            +
             | 
| 60 | 
            +
            if __name__ == "__main__":
         | 
| 61 | 
            +
                app.run(debug=True)
         | 
