|
import os |
|
from newsapi import NewsApiClient |
|
from gradio_client import Client |
|
|
|
HF_TOKEN = os.getenv("HF_TOKEN") |
|
NEWSAPI = os.getenv("NEWSAPI") |
|
|
|
|
|
|
|
def generate_image(prompt): |
|
""" |
|
generate an image based on the prompt provided |
|
""" |
|
client = Client("https://jingyechen22-textdiffuser.hf.space/") |
|
result = client.predict( |
|
prompt, |
|
20, |
|
7.5, |
|
1, |
|
"Stable Diffusion v2.1", |
|
fn_index=1) |
|
return result[0] |
|
|
|
|
|
|
|
|
|
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", |
|
input_text, |
|
input_melody, |
|
5, |
|
250, |
|
0, |
|
1, |
|
3, |
|
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"] |
|
} |
|
} |
|
|
|
|
|
|
|
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"] |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
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}, |
|
'generate_caption_func' : {'dict':generate_caption_func, 'func':generate_caption}, |
|
'get_news_func' : {'dict':get_news_func, 'func':get_news} |
|
} |
|
|