make requests async and in parallel

#2
by philschmid HF staff - opened
Files changed (1) hide show
  1. app.py +66 -36
app.py CHANGED
@@ -1,54 +1,84 @@
1
  import os
2
-
3
- import gradio as gr
4
  import requests
 
5
 
6
- TOKEN = os.environ.get("API_TOKEN")
7
 
8
- UL2_API_URL = "https://api-inference.huggingface.co/models/google/flan-ul2"
9
- FLAN_API_URL = "https://api-inference.huggingface.co/models/google/flan-t5-xxl"
 
 
 
 
10
 
11
- headers = {"Authorization": f"Bearer {TOKEN}"}
12
- MAX_NEW_TOKENS = 256
13
 
14
- def query(text, api_url):
15
- response = requests.post(api_url, headers=headers, json={"inputs":text, "parameters": {"max_new_tokens":MAX_NEW_TOKENS}})
16
- return response.json()
 
 
 
17
 
18
 
19
  examples = [
20
- ["Please answer to the following question. Who is going to be the next Ballon d'or?"],
21
- ["Q: Can Barack Obama have a conversation with George Washington? Give the rationale before answering."],
22
- ["Summarize the following text: Peter and Elizabeth took a taxi to attend the night party in the city. While in the party, Elizabeth collapsed and was rushed to the hospital. Since she was diagnosed with a brain injury, the doctor told Peter to stay besides her until she gets well. Therefore, Peter stayed with her at the hospital for 3 days without leaving."],
23
- ["Please answer the following question: What is the boiling point of water?"],
24
- ["Answer the following question by detailing your reasoning: Are Pokemons alive?"],
25
- ["Translate to German: How old are you?"],
26
- ["Generate a cooking recipe to make bolognese pasta:"],
27
- ["Answer the following yes/no question by reasoning step-by-step. Can you write a whole Haiku in a single tweet?"],
28
- ["Premise: At my age you will probably have learnt one lesson. Hypothesis: It's not certain how many lessons you'll learn by your thirties. Does the premise entail the hypothesis?"],
29
- ["Answer the following question by reasoning step by step. The cafeteria had 23 apples. If they used 20 for lunch and bought 6 more, how many apples do they have?"],
30
- ["""Q: Roger has 5 tennis balls. He buys 2 more cans of tennis balls. Each can has 3 tennis balls. How many tennis balls does he have now?
 
 
 
 
 
 
 
31
  A: Roger started with 5 balls. 2 cans of 3 tennis balls each is 6 tennis balls. 5 + 6 = 11. The answer is 11.
32
- Q: A juggler can juggle 16 balls. Half of the balls are golf balls, and half of the golf balls are blue. How many blue golf balls are there?"""]
 
33
  ]
34
 
35
  title = "Flan UL2 vs Flan T5 XXL"
36
  description = "This demo compares [Flan-T5-xxl](https://huggingface.co/google/flan-t5-xxl) and [Flan-UL2](https://huggingface.co/google/flan-ul2). Learn more about these models in their model card!"
37
 
38
- def inference(text):
39
- output_ul2 = query(text, api_url=UL2_API_URL)[0]["generated_text"]
40
- output_flan = query(text, api_url=FLAN_API_URL)[0]["generated_text"]
41
- return [output_ul2, output_flan]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
  io = gr.Interface(
44
- inference,
45
- gr.Textbox(lines=3),
46
- outputs=[
47
- gr.Textbox(lines=3, label="Flan T5-UL2"),
48
- gr.Textbox(lines=3, label="Flan T5-XXL")
49
- ],
50
- title=title,
51
- description=description,
52
- examples=examples
53
  )
54
- io.launch()
1
  import os
2
+ import asyncio
3
+ from concurrent.futures import ThreadPoolExecutor
4
  import requests
5
+ import gradio as gr
6
 
 
7
 
8
+ MAX_NEW_TOKENS = 128
9
+ TOKEN = os.environ.get("API_TOKEN", None)
10
+ URLS = [
11
+ "https://api-inference.huggingface.co/models/google/flan-ul2",
12
+ "https://api-inference.huggingface.co/models/google/flan-t5-xxl",
13
+ ]
14
 
 
 
15
 
16
+ def fetch(session, text, api_url):
17
+ model = api_url.split("/")[-1]
18
+ response = session.post(api_url, json={"inputs": text, "parameters": {"max_new_tokens": MAX_NEW_TOKENS}})
19
+ if response.status_code != 200:
20
+ return model, None
21
+ return model, response.json()
22
 
23
 
24
  examples = [
25
+ ["Please answer to the following question. Who is going to be the next Ballon d'or?"],
26
+ ["Q: Can Barack Obama have a conversation with George Washington? Give the rationale before answering."],
27
+ [
28
+ "Summarize the following text: Peter and Elizabeth took a taxi to attend the night party in the city. While in the party, Elizabeth collapsed and was rushed to the hospital. Since she was diagnosed with a brain injury, the doctor told Peter to stay besides her until she gets well. Therefore, Peter stayed with her at the hospital for 3 days without leaving."
29
+ ],
30
+ ["Please answer the following question: What is the boiling point of water?"],
31
+ ["Answer the following question by detailing your reasoning: Are Pokemons alive?"],
32
+ ["Translate to German: How old are you?"],
33
+ ["Generate a cooking recipe to make bolognese pasta:"],
34
+ ["Answer the following yes/no question by reasoning step-by-step. Can you write a whole Haiku in a single tweet?"],
35
+ [
36
+ "Premise: At my age you will probably have learnt one lesson. Hypothesis: It's not certain how many lessons you'll learn by your thirties. Does the premise entail the hypothesis?"
37
+ ],
38
+ [
39
+ "Answer the following question by reasoning step by step. The cafeteria had 23 apples. If they used 20 for lunch and bought 6 more, how many apples do they have?"
40
+ ],
41
+ [
42
+ """Q: Roger has 5 tennis balls. He buys 2 more cans of tennis balls. Each can has 3 tennis balls. How many tennis balls does he have now?
43
  A: Roger started with 5 balls. 2 cans of 3 tennis balls each is 6 tennis balls. 5 + 6 = 11. The answer is 11.
44
+ Q: A juggler can juggle 16 balls. Half of the balls are golf balls, and half of the golf balls are blue. How many blue golf balls are there?"""
45
+ ],
46
  ]
47
 
48
  title = "Flan UL2 vs Flan T5 XXL"
49
  description = "This demo compares [Flan-T5-xxl](https://huggingface.co/google/flan-t5-xxl) and [Flan-UL2](https://huggingface.co/google/flan-ul2). Learn more about these models in their model card!"
50
 
51
+
52
+ async def inference(text):
53
+ with ThreadPoolExecutor(max_workers=2) as executor:
54
+ with requests.Session() as session:
55
+ session.headers = {"Authorization": f"Bearer {TOKEN}"}
56
+ # Initialize the event loop
57
+ loop = asyncio.get_event_loop()
58
+ tasks = [
59
+ loop.run_in_executor(
60
+ executor, fetch, *(session, text, url) # Allows us to pass in multiple arguments to `fetch`
61
+ )
62
+ for url in URLS
63
+ ]
64
+
65
+ # Initializes the tasks to run and awaits their results
66
+ responses = [None, None]
67
+ for (model, response) in await asyncio.gather(*tasks):
68
+ if response is not None:
69
+ if model == "flan-ul2":
70
+ responses[0] = response[0]["generated_text"]
71
+ elif model == "flan-t5-xxl":
72
+ responses[1] = response[0]["generated_text"]
73
+ return responses
74
+
75
 
76
  io = gr.Interface(
77
+ inference,
78
+ gr.Textbox(lines=3),
79
+ outputs=[gr.Textbox(lines=3, label="Flan T5-UL2"), gr.Textbox(lines=3, label="Flan T5-XXL")],
80
+ title=title,
81
+ description=description,
82
+ examples=examples,
 
 
 
83
  )
84
+ io.launch()