mg98 commited on
Commit
9ed260f
·
verified ·
1 Parent(s): 019cdc2

Async calls to model

Browse files
Files changed (1) hide show
  1. app.py +16 -7
app.py CHANGED
@@ -1,10 +1,19 @@
1
  import gradio as gr
2
  from transformers import pipeline
 
 
 
 
3
 
4
  model_pipeline = pipeline("text2text-generation", model="tribler/dsi-search-on-toy-dataset")
5
 
6
- def process_query(query):
7
- results = model_pipeline(query, max_length=60)
 
 
 
 
 
8
  result_text = results[0]['generated_text'].strip()
9
  if result_text.startswith("http"):
10
  youtube_id = result_text.split('watch?v=')[-1]
@@ -13,8 +22,8 @@ def process_query(query):
13
  elif result_text.startswith("magnet"):
14
  return gr.HTML(f'<a href="{result_text}" target="_blank">{result_text}</a>')
15
  else:
16
- bitcoin_logo_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/4/46/Bitcoin.svg/800px-Bitcoin.svg.png"
17
- return gr.Textbox(f'<div style="display:flex;align-items:center;"><img src="{bitcoin_logo_url}" alt="Bitcoin Logo" style="width:20px;height:20px;margin-right:5px;"><span>{result_text}</span></div>')
18
 
19
  interface = gr.Interface(fn=process_query,
20
  inputs=gr.Textbox(label="Query"),
@@ -22,9 +31,9 @@ interface = gr.Interface(fn=process_query,
22
  title="Search Interface",
23
  submit_btn="Find",
24
  description="Search for movie trailers, music torrents, and bitcoin wallet addresses.",
25
- article="This interface searches a toy dataset and returns a YouTube URL, magnet link, or Bitcoin wallet address. Generative AI is slowly starting to become capable of content discovery, relevance ranking, movie creation, financial transactions, market trading, and market matchmaking. The recent advent of end-to-end generative architectures might have disruptive effect on various industries. Social media platforms, movie industry, search engines, and financial intermediaries could *in principle* be replaced with fully decentralised alternatives (think Bitcoin and Bittorrent level decentralisation). This means a shift of power towards ordinary Internet citizens. See our De-DSI paper at EuroMLSys 2024 for scientific details. Please consider contributing and joining our community around the International Workshop on Distributed Infrastructure for Common Good. ",
26
- examples=[["spider man"], ["oceans 13"], ["sister starlight"], ["bitcoin address of xileno"]])
 
27
 
28
  if __name__ == "__main__":
29
  interface.launch()
30
-
 
1
  import gradio as gr
2
  from transformers import pipeline
3
+ import asyncio
4
+ from concurrent.futures import ThreadPoolExecutor
5
+
6
+ executor = ThreadPoolExecutor()
7
 
8
  model_pipeline = pipeline("text2text-generation", model="tribler/dsi-search-on-toy-dataset")
9
 
10
+ async def async_model_pipeline(query):
11
+ loop = asyncio.get_running_loop()
12
+ results = await loop.run_in_executor(executor, model_pipeline, query, {"max_length": 60})
13
+ return results
14
+
15
+ async def process_query(query):
16
+ results = await async_model_pipeline(query)
17
  result_text = results[0]['generated_text'].strip()
18
  if result_text.startswith("http"):
19
  youtube_id = result_text.split('watch?v=')[-1]
 
22
  elif result_text.startswith("magnet"):
23
  return gr.HTML(f'<a href="{result_text}" target="_blank">{result_text}</a>')
24
  else:
25
+ bitcoin_logo_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/4/46/Bitcoin.svg/800px-Bitcoin.svg.png"
26
+ return gr.Textbox(f'<div style="display:flex;align-items:center;"><img src="{bitcoin_logo_url}" alt="Bitcoin Logo" style="width:20px;height:20px;margin-right:5px;"><span>{result_text}</span></div>')
27
 
28
  interface = gr.Interface(fn=process_query,
29
  inputs=gr.Textbox(label="Query"),
 
31
  title="Search Interface",
32
  submit_btn="Find",
33
  description="Search for movie trailers, music torrents, and bitcoin wallet addresses.",
34
+ article="This interface searches a toy dataset and returns a YouTube URL, magnet link, or Bitcoin wallet address. Generative AI is slowly starting to become capable of content discovery, relevance ranking, movie creation, financial transactions, market trading, and market matchmaking. The recent advent of end-to-end generative architectures might have disruptive effect on various industries. Social media platforms, movie industry, search engines, and financial intermediaries could *in principle* be replaced with fully decentralised alternatives (think Bitcoin and Bittorrent level decentralisation). This means a shift of power towards ordinary Internet citizens. See our De-DSI paper at EuroMLSys 2024 for scientific details. Please consider contributing and joining our community around the International Workshop on Distributed Infrastructure for Common Good.",
35
+ examples=[["spider man"], ["oceans 13"], ["sister starlight"], ["bitcoin address of xileno"]],
36
+ enable_queue=True)
37
 
38
  if __name__ == "__main__":
39
  interface.launch()