Futuresony commited on
Commit
114406d
·
verified ·
1 Parent(s): 3354f05

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -45
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 - This is likely the API key for the Gradio space itself, not the user's unique API key.
9
 
10
  # Connect to hosted space
11
- # The client initializes with the URL of the hosted Gradio space.
12
- client = Client("https://futuresony-mr-events.hf.space/") # Use the correct URL of your Gradio space
 
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
- # Validate the user's unique API key against the valid key set in your environment
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's chat function
41
- # Pass the user's unique api_key received from the request
42
- # The Gradio chat function expects query, chat_history, and api_key
43
- # We need to pass the chat_history as well, even if empty initially for a new user.
44
- # The Gradio space will manage the history based on the api_key.
45
- try:
46
- result = client.predict(
47
- query=user_message,
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": response_text}
61
 
62
  if __name__ == "__main__":
63
- # To run this in a Colab environment or similar, you might need to use
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)