-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
65 lines (50 loc) · 2.33 KB
/
app.py
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 sys
import gradio as gr
from loguru import logger
from planqk.service.client import PlanqkServiceClient
# 1. Create a new Application on the PlanQK Platform.
# 2. Subscribe to the Quantum Random Number Generator service via the PlanQK Marketplace.
# --> https://platform.planqk.de/marketplace/apis/88b46e18-3d5f-4674-ba04-0d3416c0decd
# 3. Copy the Consumer Key and Consumer Secret of your Application.
# 4. Set the environment variables CONSUMER_KEY and CONSUMER_SECRET to these values.
# 4.1 If run locally, export the variables in the terminal before running `gradio app.py`.
# 4.2 If run as a PlanQK Demo, modify the environment in the Demo Settings.
logging_level = os.environ.get("LOG_LEVEL", "DEBUG")
logger.configure(handlers=[{"sink": sys.stdout, "level": logging_level}])
logger.info("Starting Gradio Demo")
consumer_key = os.getenv("CONSUMER_KEY", None)
consumer_secret = os.getenv("CONSUMER_SECRET", None)
service_endpoint = os.getenv(
"SERVICE_ENDPOINT",
"https://gateway.platform.planqk.de/anaqor/quantum-random-number-generator/1.0.0",
)
title = "A PlanQK Demo using Gradio"
description = """This Demo is calling the
[Quantum Random Number Generator](https://platform.planqk.de/marketplace/apis/88b46e18-3d5f-4674-ba04-0d3416c0decd).
To try it out create an Application and subscribe to the service."""
def run(n_numbers: int):
client = PlanqkServiceClient(service_endpoint, consumer_key, consumer_secret)
data = {"n_numbers": n_numbers}
params = {"n_bits": 4, "backend": "qasm_simulator"}
logger.info("Calling the service with data: {}", data)
logger.info("Calling the service with params: {}", params)
logger.info("Starting execution of the service...")
job = client.start_execution(data=data, params=params)
result = client.get_result(job.id)
logger.info("Received result from the service")
random_number_list = result["result"]["random_number_list"]
logger.info("Random numbers: {}", random_number_list)
return ", ".join([str(x) for x in random_number_list])
demo = gr.Interface(
run,
gr.Number(label="The amount of numbers to be generated.", value=10),
gr.Text(
label="The random numbers generated by the service.",
),
examples=[1, 2, 3, 4, 5],
title=title,
description=description,
allow_flagging="never",
)
demo.launch()