File size: 6,464 Bytes
fb597a7 |
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
import os
from newsapi import NewsApiClient
from gradio_client import Client
HF_TOKEN = os.getenv("HF_TOKEN")
NEWSAPI = os.getenv("NEWSAPI")
# example input: prompt = "Beautiful Sky with "Gradio is love" written over it"
# defining a function to generate music using Gradio demo of TextDiffusers hosted on Spaces
def generate_image(prompt):
"""
generate an image based on the prompt provided
"""
client = Client("https://jingyechen22-textdiffuser.hf.space/")
result = client.predict(
prompt, # str in 'Input your prompt here. Please enclose keywords with 'single quotes', you may refer to the examples below. The current version only supports input in English characters.' Textbox component
20, # int | float (numeric value between 1 and 50) in 'Sampling step' Slider component
7.5, # int | float (numeric value between 1 and 9) in 'Scale of classifier-free guidance' Slider component
1, # int | float (numeric value between 1 and 4) in 'Batch size' Slider component
"Stable Diffusion v2.1", # str in 'Pre-trained Model' Radio component
fn_index=1)
return result[0]
# example input: input_text = "A cheerful country song with acoustic guitars"
# defining a function to generate music using Gradio demo of MusicGen hosted on Spaces
#input melody example = "/content/bolero_ravel.mp3"
def generate_music(input_text, input_melody ):
"""
generate music based on an input text
"""
client = Client("https://ysharma-musicgendupe.hf.space/", hf_token=HF_TOKEN)
result = client.predict(
"melody", # str in 'Model' Radio component
input_text, # str in 'Input Text' Textbox component
input_melody, # str (filepath or URL to file) in 'Melody Condition (optional)' Audio component
5, # int | float (numeric value between 1 and 120) in 'Duration' Slider component
250, # int | float in 'Top-k' Number component
0, # int | float in 'Top-p' Number component
1, # int | float in 'Temperature' Number component
3, # int | float in 'Classifier Free Guidance' Number component
fn_index=1)
return result
generate_music_func = {
"name": "generate_music",
"description": "generate music based on an input text and input melody",
"parameters": {
"type": "object",
"properties": {
"input_text": {
"type": "string",
"description": "input text for the music generation"
},
"input_melody": {
"type": "string",
"description": "file path of input melody for the music generation"
}
},
"required": ["input_text", "input_melody"]
}
}
# example input: input_image = "cat.jpg"
# defining a function to generate caption using a image caption Gradio demo hosted on Spaces
def generate_caption(input_image ):
"""
generate caption for the input image
"""
client = Client("https://nielsr-comparing-captioning-models.hf.space/")
temp = input_image.split('/')
if len(temp) == 1:
input_image = temp[0]
else:
input_image = temp[-1]
result = client.predict(
input_image,
api_name="/predict")
result = "The image can have any one of the following captions, all captions are correct: " + ", or ".join([f"'{caption.replace('.','')}'" for caption in result])
return result
generate_caption_func = {
"name": "generate_caption",
"description": "generate caption for the image present at the filepath provided",
"parameters": {
"type": "object",
"properties": {
"input_image": {
"type": "string",
"description": "filepath for the input image"
},
},
"required": ["input_image"]
}
}
generate_image_func = {
"name": "generate_image",
"description": "generate image based on the input text prompt",
"parameters": {
"type": "object",
"properties": {
"prompt": {
"type": "string",
"description": "input text prompt for the image generation"
}
},
"required": ["prompt"]
}
}
# defining a function to get the most relevant world news for a given query
# example query: Joe Biden presidency
def get_news(search_query):
"""
get top three news items for your search query
"""
newsapi = NewsApiClient(api_key=NEWSAPI)
docs = newsapi.get_everything(q=search_query,
language='en',
sort_by = 'relevancy',
page_size=3,
page=1
)['articles']
res = [news['description'] for news in docs]
res = [item.replace('<li>','').replace('</li>','').replace('<ol>','') for item in res]
res = "\n".join([f"{i}.{ res[i-1]}" for i in range(1,len(res)+1)])
return "Following list has the top three news items for the given search query : \n" + res
get_news_func = {
"name": "get_news",
"description": "get top three engilsh news items for a given query, sorted by relevancy",
"parameters": {
"type": "object",
"properties": {
"search_query": {
"type": "string",
"description": "input search string to search for relevant news"
},
},
"required": ["search_query"]
}
}
#dict_plugin_functions = { 'generate_music_func':{'dict': generate_music_func , 'func': generate_music},
# 'generate_image_func':{'dict':generate_image_func, 'func':generate_image} }
#dict_plugin_functions = { 'generate_music_func':{'dict': generate_music_func , 'func': generate_music},
# 'generate_image_func':{'dict':generate_image_func, 'func':generate_image},
# 'generate_caption_func' : {'dict':generate_caption_func, 'func':generate_caption}
# }
dict_plugin_functions = { 'generate_music_func':{'dict': generate_music_func , 'func': generate_music},
'generate_image_func':{'dict':generate_image_func, 'func':generate_image},
'generate_caption_func' : {'dict':generate_caption_func, 'func':generate_caption},
'get_news_func' : {'dict':get_news_func, 'func':get_news}
}
|