JaeSwift commited on
Commit
98fe266
1 Parent(s): 8bdd2a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -77
app.py CHANGED
@@ -1,90 +1,48 @@
1
  import gradio as gr
2
  import requests
3
 
4
- # Check internet access
5
- def check_internet_access():
6
- try:
7
- response = requests.get("https://httpbin.org/ip", timeout=5)
8
- response.raise_for_status()
9
- print("Internet access confirmed.")
10
- print("Your public IP:", response.json().get("origin"))
11
- except requests.exceptions.RequestException as e:
12
- print("No internet access:", e)
13
-
14
- # Run the internet check
15
- check_internet_access()
16
-
17
- # The rest of your horoscope bot code follows...
18
-
19
- PLACEHOLDER = """
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
- # Send a POST request with the sign and day as query parameters
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
- # Return the horoscope description if available
69
- return f"<div class='card'>{data.get('description', 'No horoscope available for today.')}</div>"
70
- except requests.exceptions.HTTPError as http_err:
71
- print(f"HTTP error occurred: {http_err}")
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(theme="soft", css=CSS) as demo:
79
- gr.Markdown(PLACEHOLDER)
80
- gr.Markdown("### Get your daily horoscope.")
81
- horoscope_output = gr.HTML() # Retains the HTML formatting for horoscopes
82
  sign_dropdown = gr.Dropdown(label="Select Your Zodiac Sign", choices=[
83
- "aries", "taurus", "gemini", "cancer", "leo", "virgo", "libra", "scorpio", "sagittarius", "capricorn", "aquarius", "pisces"
 
84
  ])
85
- btn_get_horoscope = gr.Button("Get Horoscope")
 
86
 
87
- btn_get_horoscope.click(fn=get_horoscope, inputs=sign_dropdown, outputs=horoscope_output)
 
 
88
 
89
  if __name__ == "__main__":
90
- demo.launch(server_name='0.0.0.0')
 
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")