File size: 4,489 Bytes
22614d0
 
ae72a48
 
22614d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae72a48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22614d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae72a48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from newsapi import NewsApiClient
from gradio_client import Client
import requests
import random

# 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: str) -> str:
  """
  generate an image based on the prompt provided
  """
  client = Client("https://ysharma-textdiffuser.hf.space/", hf_token="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")
  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="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")
  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


# 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


# 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='8c50d2b3d80348a2a78be021b2c49cd1')
    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


# example inputs: prompt = "I am bored what can I do?"
# defining a function to get an activity basee on activity type 
# Activity type - education, recreational, social, DIY, charity, cooking, relaxation, music, and busywork.
def bored_api(activity_type) -> str:
  """
  Get a random activity to do based on the activity type. 
  """
  activity_type_list = ["education", "recreational", "social", "diy", "charity", "cooking", "relaxation", "music", "busywork"]
  activity_type = activity_type.lower()
  if activity_type not in activity_type_list:
    activity_type = random.choice(activity_type_list)
  
  api_url = "https://www.boredapi.com/api/activity/?type=" + activity_type 
  response = requests.get(
      api_url 
      )
  return response.json()['activity']