WingNeville commited on
Commit
6b2792e
·
verified ·
1 Parent(s): 6ab55a4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -17
app.py CHANGED
@@ -2,24 +2,20 @@ import datetime
2
  import pytz
3
  import gradio as gr
4
  import os
5
- from openai import OpenAI
6
 
7
  ############################## Define tools Section ###############################
8
  def test_api_connection(query: str, api_key: str) -> str:
9
- """Tests the OpenAI API connection with the provided API key."""
10
  try:
11
- # Initialize OpenAI client with the API key
12
- client = OpenAI(api_key=api_key)
13
  # Send a test query to the model
14
- response = client.chat.completions.create(
15
- model="gpt-3.5-turbo",
16
- messages=[{"role": "user", "content": query}],
17
- max_tokens=50
18
- )
19
- # Return the response or confirmation of success
20
- return f"API connection successful! Response: {response.choices[0].message.content}"
21
  except Exception as e:
22
- return f"Error: Failed to connect to OpenAI API. Check your API key or network. Details: {str(e)}"
23
 
24
  ################################ Simple agent class #######################################
25
  class MyFirstAgent:
@@ -47,18 +43,18 @@ agent = MyFirstAgent()
47
  #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Define Gradio interface function #############################
48
  def run_agent(query):
49
  # Retrieve API key from environment variable
50
- api_key = os.getenv("OPENAI_API_KEY")
51
  if not api_key:
52
- return "Error: OPENAI_API_KEY not found in environment variables."
53
  return agent.run(query, api_key)
54
 
55
  #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Create and launch Gradio interface #########################################
56
  interface = gr.Interface(
57
  fn=run_agent,
58
- inputs=gr.Textbox(label="Enter a test query", placeholder="e.g., Hello, this is a test"),
59
  outputs=gr.Textbox(label="API Test Response"),
60
- title="My First Agent - OpenAI API Test",
61
- description="Enter a test query to verify the OpenAI API connection using the OPENAI_API_KEY from Spaces secrets."
62
  )
63
 
64
  if __name__ == "__main__":
 
2
  import pytz
3
  import gradio as gr
4
  import os
5
+ from huggingface_hub import InferenceClient
6
 
7
  ############################## Define tools Section ###############################
8
  def test_api_connection(query: str, api_key: str) -> str:
9
+ """Tests the Hugging Face Inference API connection with the provided API key."""
10
  try:
11
+ # Initialize InferenceClient with the API key
12
+ client = InferenceClient(model="distilbert-base-uncased", token=api_key)
13
  # Send a test query to the model
14
+ response = client.text_classification(query)
15
+ # Return the classification result or confirmation of success
16
+ return f"API connection successful! Response: {response}"
 
 
 
 
17
  except Exception as e:
18
+ return f"Error: Failed to connect to Hugging Face API. Check your API key or network. Details: {str(e)}"
19
 
20
  ################################ Simple agent class #######################################
21
  class MyFirstAgent:
 
43
  #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Define Gradio interface function #############################
44
  def run_agent(query):
45
  # Retrieve API key from environment variable
46
+ api_key = os.getenv("wingTokenRead")
47
  if not api_key:
48
+ return "Error: wingTokenRead not found in environment variables."
49
  return agent.run(query, api_key)
50
 
51
  #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Create and launch Gradio interface #########################################
52
  interface = gr.Interface(
53
  fn=run_agent,
54
+ inputs=gr.Textbox(label="Enter a test query", placeholder="e.g., This is a test sentence"),
55
  outputs=gr.Textbox(label="API Test Response"),
56
+ title="My First Agent - Hugging Face API Test",
57
+ description="Enter a test query to verify the Hugging Face API connection using the wingTokenRead secret."
58
  )
59
 
60
  if __name__ == "__main__":