Spaces:
Sleeping
Sleeping
File size: 2,060 Bytes
ebec1bd da88b95 ebec1bd 8033995 ebec1bd 8033995 ecf02ba 8033995 ecf02ba 8033995 ebec1bd 8033995 ebec1bd 8033995 ebec1bd |
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 |
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')
try:
# 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):
try:
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.tolist(),
limit=max_results,
)
return [
gr.Audio(
hit.payload['audio_s3url'],
label=f"style: {hit.payload['style']} -- score: {hit.score}")
for hit in hits
]
except Exception as e:
print(f"[ERROR] Exception in sound_search: {e}")
return []
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.submit(sound_search, inputs=inp, outputs=out)
demo.launch()
except Exception as e:
print(f"[ERROR] Failed to create Qdrant client: {e}")
|