Spaces:
Runtime error
Runtime error
create functions definitions
Browse files- gpt_function_definitions.py +58 -0
gpt_function_definitions.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from newsapi import NewsApiClient
|
2 |
+
from gradio_client import Client
|
3 |
+
|
4 |
+
# example input: prompt = "Beautiful Sky with "Gradio is love" written over it"
|
5 |
+
# defining a function to generate music using Gradio demo of TextDiffusers hosted on Spaces
|
6 |
+
def generate_image(prompt: str) -> str:
|
7 |
+
"""
|
8 |
+
generate an image based on the prompt provided
|
9 |
+
"""
|
10 |
+
#client = Client("https://jingyechen22-textdiffuser.hf.space/")
|
11 |
+
client = Client("https://ysharma-textdiffuser.hf.space/", hf_token="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")
|
12 |
+
result = client.predict(
|
13 |
+
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
|
14 |
+
20, # int | float (numeric value between 1 and 50) in 'Sampling step' Slider component
|
15 |
+
7.5, # int | float (numeric value between 1 and 9) in 'Scale of classifier-free guidance' Slider component
|
16 |
+
1, # int | float (numeric value between 1 and 4) in 'Batch size' Slider component
|
17 |
+
"Stable Diffusion v2.1", # str in 'Pre-trained Model' Radio component
|
18 |
+
fn_index=1)
|
19 |
+
return result[0]
|
20 |
+
|
21 |
+
|
22 |
+
# example input: input_image = "cat.jpg"
|
23 |
+
# defining a function to generate caption using a image caption Gradio demo hosted on Spaces
|
24 |
+
def generate_caption(input_image ):
|
25 |
+
"""
|
26 |
+
generate caption for the input image
|
27 |
+
"""
|
28 |
+
client = Client("https://nielsr-comparing-captioning-models.hf.space/")
|
29 |
+
temp = input_image.split('/')
|
30 |
+
if len(temp) == 1:
|
31 |
+
input_image = temp[0]
|
32 |
+
else:
|
33 |
+
input_image = temp[-1]
|
34 |
+
result = client.predict(
|
35 |
+
input_image,
|
36 |
+
api_name="/predict")
|
37 |
+
result = "The image can have any one of the following captions, all captions are correct: " + ", or ".join([f"'{caption.replace('.','')}'" for caption in result])
|
38 |
+
return result
|
39 |
+
|
40 |
+
|
41 |
+
# defining a function to get the most relevant world news for a given query
|
42 |
+
# example query: Joe Biden presidency
|
43 |
+
def get_news(search_query):
|
44 |
+
"""
|
45 |
+
get top three news items for your search query
|
46 |
+
"""
|
47 |
+
newsapi = NewsApiClient(api_key='8c50d2b3d80348a2a78be021b2c49cd1')
|
48 |
+
docs = newsapi.get_everything(q=search_query,
|
49 |
+
language='en',
|
50 |
+
sort_by = 'relevancy',
|
51 |
+
page_size=3,
|
52 |
+
page=1
|
53 |
+
)['articles']
|
54 |
+
res = [news['description'] for news in docs]
|
55 |
+
res = [item.replace('<li>','').replace('</li>','').replace('<ol>','') for item in res]
|
56 |
+
res = "\n".join([f"{i}.{ res[i-1]}" for i in range(1,len(res)+1)])
|
57 |
+
return "Following list has the top three news items for the given search query : \n" + res
|
58 |
+
|