Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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:
|
| 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 |
-
|
| 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.
|
| 58 |
-
return
|
| 59 |
|
| 60 |
-
|
| 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="
|
| 66 |
-
description="Enter a test query to verify the Hugging Face API connection
|
| 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__":
|