File size: 1,334 Bytes
0520162
d9a782e
b624ed0
 
d9a782e
b624ed0
 
d9a782e
b624ed0
 
d9a782e
 
b624ed0
 
 
d9a782e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0520162
 
947c823
d9a782e
b624ed0
 
 
 
 
a3cc6b1
b624ed0
 
 
f7b975b
b624ed0
f7b975b
 
d9a782e
b624ed0
 
d9a782e
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
import traceback
import gradio, os
from gradio_client import Client
from dotenv import load_dotenv

load_dotenv()

client = None

def redirect(query, collection_name):
    global client

    PRIVATE_SPACE_ID = os.getenv("PRIVATE_SPACE_ID")
    PRIVATE_API_KEY = os.getenv("PRIVATE_API_KEY")

    try:
        if client is None:
            client = Client(PRIVATE_SPACE_ID, hf_token=PRIVATE_API_KEY)
    except Exception as e:
        print(f"Failed to connect to the client: {e}")
        return "Failed to connect to the client. Please try again."

    try:
        result = client.predict(
            query,
            collection_name,
            api_name="/predict"
        )
    except Exception as e:
        print(f"Failed to get prediction: {e}")
        print(f"Exception type: {type(e)}")
        print(traceback.format_exc())
        return f"Failed to get prediction ({e}), Please try again."

    return result


gradio_interface = gradio.Interface(
  fn=redirect,
  api_name="search",
  inputs=["text", "text"],
  outputs="text",
  examples=[
      ["Piso", "real_bert"],
  ],
  title="TFG Web Demo Public REST API",
  description="Used by our static web app to share the real-bert model and evaluate its performance in a real-world scenario.",
  article="© Fernando Ónega Rodrigo 2024"
)

gradio_interface.launch()