File size: 2,309 Bytes
5125874
 
 
 
46e1dc5
 
5125874
 
 
 
 
 
 
 
 
 
5688d73
5125874
 
 
5688d73
 
5125874
 
46e1dc5
5125874
 
 
 
 
 
 
 
 
 
 
5688d73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5125874
46e1dc5
5125874
5688d73
 
 
5125874
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
68
69
70
71
72
73
74
import gradio as gr
import requests
import pandas as pd

def api_url(remote):
    return f"https://huynhdoo--mps-api-{remote}.modal.run"

origins = {
    'Formation' : ['formation.presentation', 'formation.summary'],
    'Métier' : ['metier.presentation', 'metier.competences',
                'metier.condition_travail', 'metier.nature_travail',
                'metier.acces_metier', 'metier.vie_professionnelle',
                'metier.accroche_metier', 'metier.format_court1',
                'metier.format_court2']
}

def retrieve(origin='Formation', query='cuisine', n_results=20):
    # Query API
    json = dict(
        query=query,
        origins=origins[origin],
        n_results=n_results
    )

    resp = requests.post(url=api_url('retrieve'), json=json)
    data = resp.json()

    # Format result
    distances = pd.DataFrame({'distance': data['distances']}) 
    metadatas = pd.DataFrame(data['metadatas'])
    documents = pd.DataFrame({'document': data['documents']})
    df = pd.concat([distances, metadatas, documents], axis=1)
    df['distance'] = df['distance'].apply(lambda x: round(x, 3))
    df['origin'] = df['origin'].apply(lambda x: x.split('.')[1])
    return df

def rank(query='cuisine', documents = []):
    # Query API
    json = dict(
        query=query,
        documents=documents
    )

    try:
        resp = requests.post(url=api_url('rank'), json=json)
        data = resp.json()
    except:
        return []

    # Format result
    return data['ranking']

gradio_app = gr.Interface(
    fn=retrieve,
    inputs=[
        gr.Dropdown(choices=list(origins.keys()), value=list(origins.keys())[0], label="Origine", info="Choisir un type de donnée à interroger"),
        gr.Textbox(label="Recherche", info="Votre recherche"),
        gr.Number(value=10, label="Nombre de résultats", info="Nombre de résultats attendus")
    ],
    outputs=[
        gr.DataFrame(label="Résultats", headers=["Distance", "Key", "Label", "Origin", "Document"])
    ],
    examples=[['Formation', 'militaire'],
              ['Métier', 'cuisine'],
              ['Formation', 'écologie'],
              ['Métier', 'eau'],
              ['Formation', 'math'],
    ],
    cache_examples=False
)

if __name__ == "__main__":
    gradio_app.launch(auth=("mps", "sup"), share=True)