mirror of
https://github.com/Ladebeze66/projetcbaollm.git
synced 2025-12-15 19:56:54 +01:00
60 lines
1.9 KiB
Python
60 lines
1.9 KiB
Python
import gradio as gr
|
|
import requests
|
|
from PIL import Image
|
|
import numpy as np
|
|
import json
|
|
import os
|
|
|
|
# 🔥 Serveur Ollama
|
|
OLLAMA_SERVER = "http://localhost:11434/api/generate"
|
|
|
|
def llama_vision_analysis(image, prompt, json_file):
|
|
if image is None:
|
|
return "❌ Veuillez fournir une image."
|
|
if not prompt:
|
|
return "❌ Veuillez entrer une question."
|
|
|
|
# 📌 Vérifier et convertir l'image (Gradio fournit parfois un numpy array)
|
|
if isinstance(image, np.ndarray):
|
|
image = Image.fromarray(image) # Convertir numpy.ndarray en Image PIL
|
|
|
|
# 📂 Sauvegarder l'image
|
|
image_path = "temp_image.jpg"
|
|
image.save(image_path)
|
|
|
|
# 📂 Lire le contenu du fichier JSON correctement
|
|
json_data = {}
|
|
if json_file is not None:
|
|
try:
|
|
json_data = json.loads(json_file) # Corrige l'erreur d'attribut
|
|
except Exception as e:
|
|
return f"⚠️ Erreur de lecture du JSON : {str(e)}"
|
|
|
|
# 📡 Construire la requête pour Ollama
|
|
request_data = {
|
|
"model": "llama3.2-vision:90b-instruct-q8_0",
|
|
"prompt": f"{prompt}\n\nAnalyse aussi ces données JSON : {json_data}",
|
|
"images": [image_path]
|
|
}
|
|
|
|
# 📨 Envoyer la requête à Ollama
|
|
try:
|
|
response = requests.post(OLLAMA_SERVER, json=request_data)
|
|
response.raise_for_status()
|
|
return response.json().get("response", "⚠️ Réponse vide")
|
|
|
|
except requests.exceptions.RequestException as e:
|
|
return f"⚠️ Erreur Ollama : {str(e)}"
|
|
|
|
# 🎨 Interface Gradio
|
|
interface = gr.Interface(
|
|
fn=llama_vision_analysis,
|
|
inputs=["image", "text", "text"], # Utiliser "text" pour JSON
|
|
outputs="text",
|
|
title="Llama-Vision 90B - Analyse Image, Texte & JSON",
|
|
description="Charge une image, un fichier JSON et entre une question pour obtenir une réponse détaillée de Llama-Vision."
|
|
)
|
|
|
|
# 🔥 Lancer Gradio
|
|
interface.launch(server_name="0.0.0.0", server_port=7860, share=True)
|