Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,11 +5,12 @@ from gradio_client import Client
|
|
| 5 |
|
| 6 |
# Keys
|
| 7 |
VALID_API_KEY = os.getenv("APP_API_KEY") # for your FastAPI security
|
| 8 |
-
SPACE_API_KEY = os.getenv("SPACE_API_KEY") # for your hosted HF Space
|
| 9 |
|
| 10 |
# Connect to hosted space
|
| 11 |
-
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
app = FastAPI()
|
| 15 |
|
|
@@ -18,54 +19,27 @@ async def chat(request: Request):
|
|
| 18 |
data = await request.json()
|
| 19 |
|
| 20 |
# API key check from headers or JSON
|
| 21 |
-
# This api_key is the unique key provided by the user/device
|
| 22 |
api_key = request.headers.get("X-API-Key") or data.get("api_key")
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
# This assumes VALID_API_KEY is the master key or a prefix validator.
|
| 26 |
-
# If you have multiple user keys, you would need a different validation mechanism here.
|
| 27 |
-
# Based on your Gradio code, it seems SECRET_API_KEY (loaded into VALID_API_KEY here) is
|
| 28 |
-
# used for validation *within* the Gradio app.
|
| 29 |
-
# So, this check might be redundant if the Gradio space handles validation,
|
| 30 |
-
# but we'll keep it if VALID_API_KEY is meant for the FastAPI endpoint itself.
|
| 31 |
-
# Let's assume the Gradio space handles the primary validation for now.
|
| 32 |
-
# if not api_key or api_key != VALID_API_KEY:
|
| 33 |
-
# raise HTTPException(status_code=403, detail="Invalid API Key")
|
| 34 |
-
|
| 35 |
|
| 36 |
user_message = data.get("message")
|
|
|
|
|
|
|
|
|
|
| 37 |
if not user_message:
|
| 38 |
raise HTTPException(status_code=400, detail="Message is required")
|
| 39 |
|
| 40 |
-
# Call your hosted Space
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
# chat_history=[], # Initial chat_history from the client can be empty; the Space loads/saves. Removed as per error
|
| 49 |
-
api_key=api_key, # Pass the user's unique API key here
|
| 50 |
-
api_name="/chat"
|
| 51 |
-
)
|
| 52 |
-
# The result from the Gradio Space is the generated response string.
|
| 53 |
-
response_text = result
|
| 54 |
-
|
| 55 |
-
except Exception as e:
|
| 56 |
-
print(f"Error calling Gradio Space: {e}")
|
| 57 |
-
raise HTTPException(status_code=500, detail=f"Error processing request: {e}")
|
| 58 |
-
|
| 59 |
|
| 60 |
-
return {"response":
|
| 61 |
|
| 62 |
if __name__ == "__main__":
|
| 63 |
-
|
| 64 |
-
# a tool like ngrok or expose the port if running on a dedicated server.
|
| 65 |
-
# For local testing within Colab, you can run it directly or use background tasks.
|
| 66 |
-
# Note: Running uvicorn directly like this will block the notebook.
|
| 67 |
-
# You might prefer using Colab's built-in features for hosting or running in a thread/process.
|
| 68 |
-
# For deployment on Hugging Face Spaces, the `app` object is typically used directly.
|
| 69 |
-
# If running locally for testing:
|
| 70 |
-
# uvicorn.run(app, host="0.0.0.0", port=7860)
|
| 71 |
-
print("FastAPI app defined. Run `uvicorn.run(app, host='0.0.0.0', port=7860)` to start the server.")
|
|
|
|
| 5 |
|
| 6 |
# Keys
|
| 7 |
VALID_API_KEY = os.getenv("APP_API_KEY") # for your FastAPI security
|
| 8 |
+
SPACE_API_KEY = os.getenv("SPACE_API_KEY") # for your hosted HF Space
|
| 9 |
|
| 10 |
# Connect to hosted space
|
| 11 |
+
client = Client("Futuresony/Mr.Events")
|
| 12 |
+
#Futuresony/Mr.Events
|
| 13 |
+
#Futuresony/ABSA_Test_Space
|
| 14 |
|
| 15 |
app = FastAPI()
|
| 16 |
|
|
|
|
| 19 |
data = await request.json()
|
| 20 |
|
| 21 |
# API key check from headers or JSON
|
|
|
|
| 22 |
api_key = request.headers.get("X-API-Key") or data.get("api_key")
|
| 23 |
+
if api_key != VALID_API_KEY:
|
| 24 |
+
raise HTTPException(status_code=403, detail="Invalid API Key")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
user_message = data.get("message")
|
| 27 |
+
chat_history = data.get("chat_history", []) # Extract chat_history from request
|
| 28 |
+
user_id = data.get("user_id") # Extract user_id from request
|
| 29 |
+
|
| 30 |
if not user_message:
|
| 31 |
raise HTTPException(status_code=400, detail="Message is required")
|
| 32 |
|
| 33 |
+
# Call your hosted Space, passing the user_id
|
| 34 |
+
result = client.predict(
|
| 35 |
+
query=user_message,
|
| 36 |
+
chat_history=chat_history, # Pass chat_history to the hosted space
|
| 37 |
+
api_key=api_key, # Pass the validated api_key
|
| 38 |
+
user_id=user_id, # Pass the user_id
|
| 39 |
+
api_name="/chat"
|
| 40 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
return {"response": result}
|
| 43 |
|
| 44 |
if __name__ == "__main__":
|
| 45 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|