Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,12 +2,13 @@ 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 |
|
@@ -22,8 +23,16 @@ def get_horoscope(sign, period="today"):
|
|
22 |
# Make the GET request to the endpoint
|
23 |
conn.request("GET", endpoint, headers=headers)
|
24 |
res = conn.getresponse()
|
25 |
-
data = res.read()
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
# Function to generate an image from the horoscope text
|
29 |
def generate_horoscope_image(text):
|
|
|
2 |
import http.client
|
3 |
from PIL import Image, ImageDraw, ImageFont
|
4 |
import urllib.parse
|
5 |
+
import json
|
6 |
|
7 |
# RapidAPI credentials and host
|
8 |
API_KEY = "2e427e3d07mshba1bdb10cb6eb30p12d12fjsn215dd7746115" # Replace with your actual API key
|
9 |
API_HOST = "horoscopes-ai.p.rapidapi.com"
|
10 |
|
11 |
+
# Function to fetch and parse horoscope based on sign and period
|
12 |
def get_horoscope(sign, period="today"):
|
13 |
conn = http.client.HTTPSConnection(API_HOST)
|
14 |
|
|
|
23 |
# Make the GET request to the endpoint
|
24 |
conn.request("GET", endpoint, headers=headers)
|
25 |
res = conn.getresponse()
|
26 |
+
data = res.read().decode("utf-8")
|
27 |
+
|
28 |
+
# Parse the JSON response and extract the horoscope text
|
29 |
+
try:
|
30 |
+
response_data = json.loads(data)
|
31 |
+
horoscope_text = response_data.get("general", ["No horoscope available"])[0]
|
32 |
+
except json.JSONDecodeError:
|
33 |
+
horoscope_text = "Error: Unable to parse horoscope data."
|
34 |
+
|
35 |
+
return horoscope_text
|
36 |
|
37 |
# Function to generate an image from the horoscope text
|
38 |
def generate_horoscope_image(text):
|