File size: 2,119 Bytes
e562a59
0d3e4a3
 
 
a2b1b52
36b8886
e562a59
a2b1b52
 
c9a464e
36b8886
a2b1b52
 
8bc75c1
 
 
 
36b8886
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import os
import gradio as gr
from qdrant_client import QdrantClient
from transformers import ClapModel, ClapProcessor
from dotenv import load_dotenv
import requests

# Charger les variables d'environnement à partir du fichier .env
load_dotenv()

# Récupérer les variables d'environnement
QDRANT_URL = os.getenv('QDRANT_URL')
QDRANT_KEY = os.getenv('QDRANT_KEY')

# Vérifier les valeurs récupérées
print(f"QDRANT_URL: {QDRANT_URL}")
print(f"QDRANT_KEY: {QDRANT_KEY}")

try:
    # Tester la connexion à l'URL de Qdrant
    response = requests.get(QDRANT_URL)
    print(f"Test de la connexion à Qdrant: {response.status_code}")

    # Vérifier que les variables sont correctement récupérées
    if not QDRANT_URL or not QDRANT_KEY:
        raise ValueError("Les variables d'environnement QDRANT_URL ou QDRANT_KEY ne sont pas définies")

    # Connexion au client Qdrant
    client = QdrantClient(QDRANT_URL, api_key=QDRANT_KEY)
    print("[INFO] Client created...")

    # Chargement du modèle
    print("[INFO] Loading the model...")
    model_name = "laion/larger_clap_general"
    model = ClapModel.from_pretrained(model_name)
    processor = ClapProcessor.from_pretrained(model_name)

    # Interface Gradio
    max_results = 10

    def sound_search(query):
        text_inputs = processor(text=query, return_tensors="pt")
        text_embed = model.get_text_features(**text_inputs)[0]

        hits = client.search(
            collection_name="demo_spaces_db",
            query_vector=text_embed,
            limit=max_results,
        )
        return [
            gr.Audio(
                hit.payload['audio_path'],
                label=f"style: {hit.payload['style']} -- score: {hit.score}")
            for hit in hits
        ]

    with gr.Blocks() as demo:
        gr.Markdown("# Sound search database")
        inp = gr.Textbox(placeholder="What sound are you looking for ?")
        out = [gr.Audio(label=f"{x}") for x in range(max_results)]
        inp.change(sound_search, inp, out)

    demo.launch()

except Exception as e:
    print(f"[ERROR] Failed to create Qdrant client: {e}")