JaeSwift commited on
Commit
86bc9d4
·
verified ·
1 Parent(s): 47b47c1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -6
app.py CHANGED
@@ -1,14 +1,15 @@
1
  import gradio as gr
2
  import requests
 
 
3
 
4
  # RapidAPI Credentials and API Endpoint
5
  API_KEY = "2e427e3d07mshba1bdb10cb6eb30p12d12fjsn215dd7746115" # Replace with your actual API key
6
  API_HOST = "horoscopes-ai.p.rapidapi.com"
7
  API_URL_TEMPLATE = "https://horoscopes-ai.p.rapidapi.com/get_horoscope/{sign}/{period}/general/en"
8
 
9
- # Function to Fetch Horoscope
10
  def get_horoscope(sign, period="today"):
11
- # Construct the URL based on the selected sign and period
12
  url = API_URL_TEMPLATE.format(sign=sign, period=period)
13
 
14
  headers = {
@@ -16,7 +17,6 @@ def get_horoscope(sign, period="today"):
16
  "x-rapidapi-host": API_HOST
17
  }
18
 
19
- # Send GET request to the API
20
  try:
21
  response = requests.get(url, headers=headers)
22
  response.raise_for_status()
@@ -24,25 +24,97 @@ def get_horoscope(sign, period="today"):
24
  # Parse JSON response and retrieve the horoscope text
25
  data = response.json()
26
  horoscope_text = data.get("general", ["No horoscope available"])[0]
27
- return horoscope_text
 
 
 
 
 
 
 
28
  except requests.exceptions.RequestException as e:
29
- return f"Error retrieving horoscope: {e}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  # Gradio Interface Setup
32
  with gr.Blocks() as demo:
33
  gr.Markdown("<center><h1>Daily Horoscope by Enemy AI</h1></center>")
34
  gr.Markdown("Select your zodiac sign and period to receive your personalized horoscope.")
35
 
 
36
  sign_dropdown = gr.Dropdown(label="Select Your Zodiac Sign", choices=[
37
  "aries", "taurus", "gemini", "cancer", "leo", "virgo",
38
  "libra", "scorpio", "sagittarius", "capricorn", "aquarius", "pisces"
39
  ])
40
  period_dropdown = gr.Dropdown(label="Select Period", choices=["today", "tomorrow", "yesterday"], value="today")
 
 
41
  horoscope_output = gr.Textbox(label="Your Horoscope")
42
 
 
 
 
 
 
 
43
  # Button to trigger the API call
44
  btn_get_horoscope = gr.Button("Get Horoscope")
45
- btn_get_horoscope.click(fn=get_horoscope, inputs=[sign_dropdown, period_dropdown], outputs=horoscope_output)
 
 
 
 
 
 
 
 
 
 
 
46
 
47
  if __name__ == "__main__":
48
  demo.launch(server_name="0.0.0.0")
 
1
  import gradio as gr
2
  import requests
3
+ import urllib.parse
4
+ from PIL import Image, ImageDraw, ImageFont
5
 
6
  # RapidAPI Credentials and API Endpoint
7
  API_KEY = "2e427e3d07mshba1bdb10cb6eb30p12d12fjsn215dd7746115" # Replace with your actual API key
8
  API_HOST = "horoscopes-ai.p.rapidapi.com"
9
  API_URL_TEMPLATE = "https://horoscopes-ai.p.rapidapi.com/get_horoscope/{sign}/{period}/general/en"
10
 
11
+ # Function to Fetch Horoscope and Create Twitter Share Link
12
  def get_horoscope(sign, period="today"):
 
13
  url = API_URL_TEMPLATE.format(sign=sign, period=period)
14
 
15
  headers = {
 
17
  "x-rapidapi-host": API_HOST
18
  }
19
 
 
20
  try:
21
  response = requests.get(url, headers=headers)
22
  response.raise_for_status()
 
24
  # Parse JSON response and retrieve the horoscope text
25
  data = response.json()
26
  horoscope_text = data.get("general", ["No horoscope available"])[0]
27
+
28
+ # Prepare the Twitter share link
29
+ share_text = f"Here's my horoscope by Enemy AI for {sign.capitalize()} on {period.capitalize()}:\n{horoscope_text}"
30
+ encoded_text = urllib.parse.quote(share_text) # URL-encode the text
31
+ twitter_share_url = f"https://twitter.com/intent/tweet?text={encoded_text}"
32
+
33
+ # Return both the horoscope and the Twitter link
34
+ return horoscope_text, twitter_share_url
35
  except requests.exceptions.RequestException as e:
36
+ return f"Error retrieving horoscope: {e}", None
37
+
38
+ # Function to Generate Image from Horoscope Text
39
+ def generate_horoscope_image(text):
40
+ # Define image size and background color
41
+ width, height = 800, 400
42
+ background_color = "lightblue"
43
+
44
+ # Create a blank image with background color
45
+ image = Image.new("RGB", (width, height), color=background_color)
46
+ draw = ImageDraw.Draw(image)
47
+
48
+ # Set font and text color
49
+ try:
50
+ # Use a default PIL font if no custom font is available
51
+ font = ImageFont.truetype("arial.ttf", size=20)
52
+ except IOError:
53
+ font = ImageFont.load_default()
54
+
55
+ text_color = "black"
56
+ padding = 20
57
+
58
+ # Wrap text to fit within the image width
59
+ lines = []
60
+ words = text.split()
61
+ while words:
62
+ line = ''
63
+ while words and (draw.textsize(line + words[0], font=font)[0] <= width - padding * 2):
64
+ line += (words.pop(0) + ' ')
65
+ lines.append(line)
66
+
67
+ # Calculate text position for centered alignment
68
+ total_text_height = sum([draw.textsize(line, font=font)[1] for line in lines])
69
+ y_text = (height - total_text_height) // 2
70
+
71
+ # Draw each line of text
72
+ for line in lines:
73
+ text_width, text_height = draw.textsize(line, font=font)
74
+ x_text = (width - text_width) // 2 # Center align
75
+ draw.text((x_text, y_text), line, font=font, fill=text_color)
76
+ y_text += text_height
77
+
78
+ # Save image to file
79
+ image_path = "/tmp/horoscope_image.png"
80
+ image.save(image_path)
81
+ return image_path
82
 
83
  # Gradio Interface Setup
84
  with gr.Blocks() as demo:
85
  gr.Markdown("<center><h1>Daily Horoscope by Enemy AI</h1></center>")
86
  gr.Markdown("Select your zodiac sign and period to receive your personalized horoscope.")
87
 
88
+ # Input dropdowns for sign and period
89
  sign_dropdown = gr.Dropdown(label="Select Your Zodiac Sign", choices=[
90
  "aries", "taurus", "gemini", "cancer", "leo", "virgo",
91
  "libra", "scorpio", "sagittarius", "capricorn", "aquarius", "pisces"
92
  ])
93
  period_dropdown = gr.Dropdown(label="Select Period", choices=["today", "tomorrow", "yesterday"], value="today")
94
+
95
+ # Textbox to display the horoscope
96
  horoscope_output = gr.Textbox(label="Your Horoscope")
97
 
98
+ # Link to share on Twitter
99
+ twitter_link = gr.HTML()
100
+
101
+ # Image output for download
102
+ horoscope_image = gr.Image(label="Downloadable Horoscope Image")
103
+
104
  # Button to trigger the API call
105
  btn_get_horoscope = gr.Button("Get Horoscope")
106
+
107
+ # Define the click event
108
+ def on_click(sign, period):
109
+ horoscope_text, twitter_share_url = get_horoscope(sign, period)
110
+ twitter_button_html = f'<a href="{twitter_share_url}" target="_blank" style="color: white; background-color: #1DA1F2; padding: 10px; border-radius: 5px; text-decoration: none;">Share on Twitter</a>'
111
+
112
+ # Generate horoscope image and return path for download
113
+ image_path = generate_horoscope_image(horoscope_text)
114
+ return horoscope_text, twitter_button_html, image_path
115
+
116
+ # Set up click action to update outputs
117
+ btn_get_horoscope.click(fn=on_click, inputs=[sign_dropdown, period_dropdown], outputs=[horoscope_output, twitter_link, horoscope_image])
118
 
119
  if __name__ == "__main__":
120
  demo.launch(server_name="0.0.0.0")