raomyousaf commited on
Commit
2d4d1cc
·
verified ·
1 Parent(s): e4ea002

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -12
app.py CHANGED
@@ -1,39 +1,53 @@
1
  import streamlit as st
2
  import requests
3
 
4
- # Function to fetch data from Hugging Face API
5
- def fetch_huggingface_data(api_url, headers=None):
6
  try:
7
- response = requests.get(api_url, headers=headers)
 
8
  if response.status_code == 200:
9
  return response.json()
10
  else:
11
  st.error(f"Failed to fetch data: {response.status_code} - {response.text}")
12
  return None
 
 
 
 
 
 
 
 
13
  except Exception as e:
14
- st.error(f"An error occurred: {str(e)}")
15
  return None
16
 
17
  # Streamlit app
18
  def main():
19
- st.title("Hugging Face API Data Viewer")
20
-
21
- # Input fields
 
 
 
 
22
  api_url = st.text_input("Enter API URL", placeholder="https://api.example.com/data")
23
  api_key = st.text_input("Enter API Key (if required)", type="password", placeholder="Optional")
24
 
 
25
  if st.button("Fetch Data"):
26
  if api_url:
27
- # Set headers for Hugging Face API
28
  headers = {"Authorization": f"Bearer {api_key}"} if api_key else None
29
- data = fetch_huggingface_data(api_url, headers)
30
 
31
  if data:
32
  st.success("Data fetched successfully!")
33
  st.json(data) # Display raw JSON data
34
-
35
- # Optionally process and display specific fields in a table
36
- if isinstance(data, list):
37
  st.write("Structured Data View:")
38
  st.dataframe(data)
39
  else:
 
1
  import streamlit as st
2
  import requests
3
 
4
+ # Function to fetch data from API
5
+ def fetch_api_data(api_url, headers=None):
6
  try:
7
+ # Bypass SSL verification for testing (set verify=False). Use with caution!
8
+ response = requests.get(api_url, headers=headers, verify=False)
9
  if response.status_code == 200:
10
  return response.json()
11
  else:
12
  st.error(f"Failed to fetch data: {response.status_code} - {response.text}")
13
  return None
14
+ except requests.exceptions.SSLError as ssl_error:
15
+ st.error("SSL Error: Certificate verification failed.")
16
+ st.error(str(ssl_error))
17
+ return None
18
+ except requests.exceptions.ConnectionError as conn_error:
19
+ st.error("Connection Error: Unable to reach the server.")
20
+ st.error(str(conn_error))
21
+ return None
22
  except Exception as e:
23
+ st.error(f"An unexpected error occurred: {str(e)}")
24
  return None
25
 
26
  # Streamlit app
27
  def main():
28
+ st.title("API Data Viewer")
29
+ st.markdown("""
30
+ This app allows you to fetch and display data from an API.
31
+ If the server uses an invalid SSL certificate, SSL verification can be bypassed for testing purposes.
32
+ """)
33
+
34
+ # Input API URL and optional API key
35
  api_url = st.text_input("Enter API URL", placeholder="https://api.example.com/data")
36
  api_key = st.text_input("Enter API Key (if required)", type="password", placeholder="Optional")
37
 
38
+ # Fetch Data button
39
  if st.button("Fetch Data"):
40
  if api_url:
41
+ # Set headers if API key is provided
42
  headers = {"Authorization": f"Bearer {api_key}"} if api_key else None
43
+ data = fetch_api_data(api_url, headers)
44
 
45
  if data:
46
  st.success("Data fetched successfully!")
47
  st.json(data) # Display raw JSON data
48
+
49
+ # Optionally show structured data in a table if the response is a list of objects
50
+ if isinstance(data, list) and all(isinstance(item, dict) for item in data):
51
  st.write("Structured Data View:")
52
  st.dataframe(data)
53
  else: