Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,24 +2,20 @@ import datetime
|
|
| 2 |
import pytz
|
| 3 |
import gradio as gr
|
| 4 |
import os
|
| 5 |
-
from
|
| 6 |
|
| 7 |
############################## Define tools Section ###############################
|
| 8 |
def test_api_connection(query: str, api_key: str) -> str:
|
| 9 |
-
"""Tests the
|
| 10 |
try:
|
| 11 |
-
# Initialize
|
| 12 |
-
client =
|
| 13 |
# Send a test query to the model
|
| 14 |
-
response = client.
|
| 15 |
-
|
| 16 |
-
|
| 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
|
| 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("
|
| 51 |
if not api_key:
|
| 52 |
-
return "Error:
|
| 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.,
|
| 59 |
outputs=gr.Textbox(label="API Test Response"),
|
| 60 |
-
title="My First Agent -
|
| 61 |
-
description="Enter a test query to verify the
|
| 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__":
|