EliteChatGPT-Plugins-in-Gradio / gpt_function_definitions.py
pikto's picture
Duplicate from ysharma/ChatGPT-Plugins-in-Gradio
fb597a7
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}
}