Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,90 +1,48 @@
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
|
4 |
-
#
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
<center>
|
21 |
-
<p><strong>Daily Horoscope by Enemy AI</strong></p>
|
22 |
-
</center>
|
23 |
-
"""
|
24 |
-
|
25 |
-
CSS = """
|
26 |
-
.card {
|
27 |
-
border: 1px solid black;
|
28 |
-
border-radius: 10px;
|
29 |
-
padding: 10px;
|
30 |
-
text-align: center;
|
31 |
-
box-shadow: 5px 5px 15px rgba(0, 0, 0, 0.3);
|
32 |
-
margin: 10px;
|
33 |
-
}
|
34 |
-
h3 {
|
35 |
-
text-align: center;
|
36 |
-
}
|
37 |
-
.button-container {
|
38 |
-
text-align: center;
|
39 |
-
margin-top: 20px;
|
40 |
-
}
|
41 |
-
.result-container {
|
42 |
-
margin-top: 20px;
|
43 |
-
}
|
44 |
-
"""
|
45 |
-
|
46 |
-
# Aztro API URL
|
47 |
-
AZTRO_API_URL = "https://aztro.sameerkumar.website/"
|
48 |
-
|
49 |
-
# Horoscope Function with exact query parameter formatting
|
50 |
-
def get_horoscope(sign):
|
51 |
try:
|
52 |
-
|
53 |
-
params = {
|
54 |
-
'sign': sign,
|
55 |
-
'day': 'today'
|
56 |
-
}
|
57 |
-
response = requests.post(AZTRO_API_URL, params=params)
|
58 |
-
|
59 |
-
# Debugging statements to inspect response
|
60 |
-
print(f"Request URL: {response.url}")
|
61 |
-
print(f"Status Code: {response.status_code}")
|
62 |
-
print(f"Response Text: {response.text}")
|
63 |
-
|
64 |
-
# Raise an error for non-200 status codes
|
65 |
response.raise_for_status()
|
66 |
|
|
|
67 |
data = response.json()
|
68 |
-
|
69 |
-
return
|
70 |
-
except requests.exceptions.
|
71 |
-
|
72 |
-
return "<div class='card'>Error retrieving horoscope. Please try again later.</div>"
|
73 |
-
except requests.exceptions.RequestException as req_err:
|
74 |
-
print(f"Request error occurred: {req_err}")
|
75 |
-
return "<div class='card'>Error retrieving horoscope. Please try again later.</div>"
|
76 |
|
77 |
-
# Gradio Interface
|
78 |
-
with gr.Blocks(
|
79 |
-
gr.Markdown(
|
80 |
-
gr.Markdown("
|
81 |
-
|
82 |
sign_dropdown = gr.Dropdown(label="Select Your Zodiac Sign", choices=[
|
83 |
-
"aries", "taurus", "gemini", "cancer", "leo", "virgo",
|
|
|
84 |
])
|
85 |
-
|
|
|
86 |
|
87 |
-
|
|
|
|
|
88 |
|
89 |
if __name__ == "__main__":
|
90 |
-
demo.launch(server_name=
|
|
|
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 = {
|
15 |
+
"x-rapidapi-key": API_KEY,
|
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()
|
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 |
+
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")
|