philschmid HF staff commited on
Commit
04c3d31
1 Parent(s): ab22cce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -50
app.py CHANGED
@@ -6,73 +6,79 @@ 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
  def fetch(session, text, api_url):
16
  model = api_url.split("/")[-1]
17
- response = session.post(api_url, json={"inputs":text, "parameters": {"max_new_tokens":MAX_NEW_TOKENS}})
18
  if response.status_code != 200:
19
- return None
20
  return model, response.json()
21
 
22
 
23
  examples = [
24
- ["Please answer to the following question. Who is going to be the next Ballon d'or?"],
25
- ["Q: Can Barack Obama have a conversation with George Washington? Give the rationale before answering."],
26
- ["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."],
27
- ["Please answer the following question: What is the boiling point of water?"],
28
- ["Answer the following question by detailing your reasoning: Are Pokemons alive?"],
29
- ["Translate to German: How old are you?"],
30
- ["Generate a cooking recipe to make bolognese pasta:"],
31
- ["Answer the following yes/no question by reasoning step-by-step. Can you write a whole Haiku in a single tweet?"],
32
- ["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?"],
33
- ["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?"],
34
- ["""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?
 
 
 
 
 
 
 
35
  A: Roger started with 5 balls. 2 cans of 3 tennis balls each is 6 tennis balls. 5 + 6 = 11. The answer is 11.
36
- 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?"""]
 
37
  ]
38
 
39
  title = "Flan UL2 vs Flan T5 XXL"
40
  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!"
41
 
 
42
  async def inference(text):
43
- with ThreadPoolExecutor(max_workers=2) as executor:
44
- with requests.Session() as session:
45
- session.headers = {"Authorization": f"Bearer {TOKEN}"}
46
- # Initialize the event loop
47
- loop = asyncio.get_event_loop()
48
- tasks = [
49
- loop.run_in_executor(
50
- executor,
51
- fetch,
52
- *(session, text, url) # Allows us to pass in multiple arguments to `fetch`
53
- )
54
- for url in URLS
55
- ]
56
-
57
- # Initializes the tasks to run and awaits their results
58
- responses = [None, None]
59
- for (model, response) in await asyncio.gather(*tasks):
60
- if response is not None:
61
- if model == "flan-ul2":
62
- responses[0] = response
63
- elif model == "flan-t5-xxl":
64
- responses[1] = response
65
- return responses
66
 
67
  io = gr.Interface(
68
- inference,
69
- gr.Textbox(lines=3),
70
- outputs=[
71
- gr.Textbox(lines=3, label="Flan T5-UL2"),
72
- gr.Textbox(lines=3, label="Flan T5-XXL")
73
- ],
74
- title=title,
75
- description=description,
76
- examples=examples
77
  )
78
- io.launch()
 
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()