JaeSwift commited on
Commit
461c8a5
·
verified ·
1 Parent(s): 0b8c5fb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -34
app.py CHANGED
@@ -1,43 +1,33 @@
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 = {
16
- "x-rapidapi-key": API_KEY,
17
- "x-rapidapi-host": API_HOST
18
  }
19
 
20
- try:
21
- response = requests.get(url, headers=headers)
22
- response.raise_for_status()
23
-
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 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
 
@@ -47,7 +37,6 @@ def generate_horoscope_image(text):
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()
@@ -64,7 +53,7 @@ def generate_horoscope_image(text):
64
  line += (words.pop(0) + ' ')
65
  lines.append(line)
66
 
67
- # Calculate text position for centered alignment
68
  total_text_height = sum([draw.textbbox((0, 0), line, font=font)[3] - draw.textbbox((0, 0), line, font=font)[1] for line in lines])
69
  y_text = (height - total_text_height) // 2
70
 
@@ -75,7 +64,7 @@ def generate_horoscope_image(text):
75
  draw.text((x_text, y_text), line, font=font, fill=text_color)
76
  y_text += draw.textbbox((0, 0), line, font=font)[3] - draw.textbbox((0, 0), line, font=font)[1]
77
 
78
- # Save image to file
79
  image_path = "/tmp/horoscope_image.png"
80
  image.save(image_path)
81
  return image_path
@@ -104,16 +93,22 @@ with gr.Blocks() as demo:
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__":
 
1
  import gradio as gr
2
+ import http.client
 
3
  from PIL import Image, ImageDraw, ImageFont
4
+ import urllib.parse
5
 
6
+ # RapidAPI credentials and host
7
  API_KEY = "2e427e3d07mshba1bdb10cb6eb30p12d12fjsn215dd7746115" # Replace with your actual API key
8
  API_HOST = "horoscopes-ai.p.rapidapi.com"
 
9
 
10
+ # Function to fetch horoscope based on sign and period
11
  def get_horoscope(sign, period="today"):
12
+ conn = http.client.HTTPSConnection(API_HOST)
13
 
14
  headers = {
15
+ 'x-rapidapi-key': API_KEY,
16
+ 'x-rapidapi-host': API_HOST
17
  }
18
 
19
+ # Construct the endpoint with the selected sign and period
20
+ endpoint = f"/get_horoscope_en/{sign}/{period}/general"
21
+
22
+ # Make the GET request to the endpoint
23
+ conn.request("GET", endpoint, headers=headers)
24
+ res = conn.getresponse()
25
+ data = res.read()
26
+ return data.decode("utf-8")
 
 
 
 
 
 
 
 
 
27
 
28
+ # Function to generate an image from the horoscope text
29
  def generate_horoscope_image(text):
30
+ # Set image size and background color
31
  width, height = 800, 400
32
  background_color = "lightblue"
33
 
 
37
 
38
  # Set font and text color
39
  try:
 
40
  font = ImageFont.truetype("arial.ttf", size=20)
41
  except IOError:
42
  font = ImageFont.load_default()
 
53
  line += (words.pop(0) + ' ')
54
  lines.append(line)
55
 
56
+ # Calculate vertical position for centered text
57
  total_text_height = sum([draw.textbbox((0, 0), line, font=font)[3] - draw.textbbox((0, 0), line, font=font)[1] for line in lines])
58
  y_text = (height - total_text_height) // 2
59
 
 
64
  draw.text((x_text, y_text), line, font=font, fill=text_color)
65
  y_text += draw.textbbox((0, 0), line, font=font)[3] - draw.textbbox((0, 0), line, font=font)[1]
66
 
67
+ # Save image to a temporary path
68
  image_path = "/tmp/horoscope_image.png"
69
  image.save(image_path)
70
  return image_path
 
93
  # Button to trigger the API call
94
  btn_get_horoscope = gr.Button("Get Horoscope")
95
 
96
+ # Define the button click event
97
  def on_click(sign, period):
98
+ horoscope_text = get_horoscope(sign, period)
99
+
100
+ # Prepare Twitter share link
101
+ share_text = f"Here's my horoscope for {sign.capitalize()} on {period.capitalize()}:\n{horoscope_text}"
102
+ encoded_text = urllib.parse.quote(share_text)
103
+ twitter_share_url = f"https://twitter.com/intent/tweet?text={encoded_text}"
104
  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>'
105
 
106
+ # Generate horoscope image for download
107
  image_path = generate_horoscope_image(horoscope_text)
108
+
109
  return horoscope_text, twitter_button_html, image_path
110
 
111
+ # Set up Gradio click action
112
  btn_get_horoscope.click(fn=on_click, inputs=[sign_dropdown, period_dropdown], outputs=[horoscope_output, twitter_link, horoscope_image])
113
 
114
  if __name__ == "__main__":