WingNeville commited on
Commit
1b21bef
·
verified ·
1 Parent(s): 8df8377

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -47
app.py CHANGED
@@ -1,69 +1,31 @@
1
- import datetime
2
- import pytz
3
- import gradio as gr
4
  import os
5
  from huggingface_hub import InferenceClient
 
6
 
7
-
8
-
9
- api_key = os.getenv("WingTokenAll")
10
- print(f"API Key Retrieved: {api_key is not None}") # Debug line
11
-
12
-
13
-
14
- ############################## Define tools Section ###############################
15
  def test_api_connection(query: str, api_key: str) -> str:
16
  """Tests the Hugging Face Inference API connection with the provided API key."""
17
  try:
18
- # Initialize InferenceClient with the API key
19
  client = InferenceClient(model="distilbert-base-uncased-finetuned-sst-2-english", token=api_key)
20
- # Send a test query to the model
21
  response = client.text_classification(query)
22
- # Return the classification result or confirmation of success
23
  return f"API connection successful! Response: {response}"
24
  except Exception as e:
25
- return f"Error: Failed to connect to Hugging Face API. Details: {str(e)}\n\nDebugging steps:\n1. Verify 'wingTokenRead' in Spaces Settings > Repository secrets matches the token from https://huggingface.co/settings/tokens.\n2. Ensure the token has read permissions and is not expired.\n3. Rebuild the Space to apply changes.\n4. If the issue persists, try a new token with read/write permissions."
26
-
27
- ################################ Simple agent class #######################################
28
- class MyFirstAgent:
29
-
30
- ###instance
31
- def __init__(self):
32
- self.tools = {
33
- "test_api": test_api_connection
34
- }
35
-
36
- def run(self, query, api_key):
37
-
38
-
39
- query = query.lower().strip()
40
-
41
- # Route to the API test tool
42
- try:
43
- if not query:
44
- return "Error: Please provide a non-empty query."
45
- return self.tools["test_api"](query, api_key)
46
- except:
47
- return "Error: Failed to process the query. Ensure the query and API key are valid."
48
-
49
- #################################### Create agent ##############################
50
- agent = MyFirstAgent()
51
 
52
- #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Define Gradio interface function #############################
53
  def run_agent(query):
54
- # Retrieve API key from environment variable
55
  api_key = os.getenv("WingTokenAll")
56
  if not api_key:
57
- return "Error: wingTokenRead not found in environment variables. Please set it in Spaces Settings > Repository secrets at https://huggingface.co/spaces."
58
- return agent.run(query, api_key)
59
 
60
- #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Create and launch Gradio interface #########################################
61
  interface = gr.Interface(
62
  fn=run_agent,
63
  inputs=gr.Textbox(label="Enter a test query", placeholder="e.g., This is a test sentence"),
64
  outputs=gr.Textbox(label="API Test Response"),
65
- title="My First Agent - Hugging Face API Test",
66
- description="Enter a test query to verify the Hugging Face API connection using the wingTokenRead secret. Created on May 29, 2025, 11:42 PM HKT."
67
  )
68
 
69
  if __name__ == "__main__":
 
 
 
 
1
  import os
2
  from huggingface_hub import InferenceClient
3
+ import gradio as gr
4
 
5
+ # Function to test API connection
 
 
 
 
 
 
 
6
  def test_api_connection(query: str, api_key: str) -> str:
7
  """Tests the Hugging Face Inference API connection with the provided API key."""
8
  try:
 
9
  client = InferenceClient(model="distilbert-base-uncased-finetuned-sst-2-english", token=api_key)
 
10
  response = client.text_classification(query)
 
11
  return f"API connection successful! Response: {response}"
12
  except Exception as e:
13
+ return f"Error: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
+ # Gradio interface function
16
  def run_agent(query):
 
17
  api_key = os.getenv("WingTokenAll")
18
  if not api_key:
19
+ return "Error: wingTokenRead not found in environment variables."
20
+ return test_api_connection(query, api_key)
21
 
22
+ # Create and launch Gradio interface
23
  interface = gr.Interface(
24
  fn=run_agent,
25
  inputs=gr.Textbox(label="Enter a test query", placeholder="e.g., This is a test sentence"),
26
  outputs=gr.Textbox(label="API Test Response"),
27
+ title="Hugging Face API Test",
28
+ description="Enter a test query to verify the Hugging Face API connection."
29
  )
30
 
31
  if __name__ == "__main__":