engasm89 commited on
Commit
2a8521a
·
verified ·
1 Parent(s): 32992a1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -10
app.py CHANGED
@@ -1,28 +1,56 @@
1
  from fastapi import FastAPI
 
2
  from langchain_openai import ChatOpenAI
3
  from pydantic import BaseModel
4
  import os
5
 
6
- # Initialize FastAPI app
7
  app = FastAPI()
8
 
9
- # Load API key from Hugging Face Secrets
10
- OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") # Set this in Hugging Face secrets
11
 
12
- # Initialize OpenAI model
13
  llm = ChatOpenAI(model_name="gpt-4", openai_api_key=OPENAI_API_KEY)
14
 
15
- # Define request model
16
  class QueryRequest(BaseModel):
17
  prompt: str
18
 
19
- # API endpoint for AI queries
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  @app.post("/query")
21
- def query_llm(request: QueryRequest):
22
- response = llm.predict(request.prompt)
 
 
 
 
 
 
 
 
 
23
  return {"response": response}
24
 
25
- # Run the FastAPI server
26
  if __name__ == "__main__":
27
  import uvicorn
28
- uvicorn.run(app, host="0.0.0.0", port=7860) # Hugging Face Spaces uses port 7860
 
1
  from fastapi import FastAPI
2
+ import asyncio
3
  from langchain_openai import ChatOpenAI
4
  from pydantic import BaseModel
5
  import os
6
 
7
+ # Initialize FastAPI application
8
  app = FastAPI()
9
 
10
+ # Load OpenAI API key from Hugging Face secrets/environment variables
11
+ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") # Ensure you set this in Hugging Face secrets
12
 
13
+ # Initialize OpenAI model (async-compatible)
14
  llm = ChatOpenAI(model_name="gpt-4", openai_api_key=OPENAI_API_KEY)
15
 
16
+ # Define a request model for structured API input
17
  class QueryRequest(BaseModel):
18
  prompt: str
19
 
20
+ # Root endpoint for basic API status check
21
+ @app.get("/")
22
+ async def home():
23
+ """ Root endpoint to confirm API is running. """
24
+ return {"message": "FastAPI is running! Use /query for AI responses."}
25
+
26
+ # Asynchronous endpoint to simulate AI response delay
27
+ @app.get("/async-query")
28
+ async def async_query():
29
+ """
30
+ Simulates an async AI query by delaying the response for 2 seconds.
31
+
32
+ Returns:
33
+ JSON response after a delay.
34
+ """
35
+ await asyncio.sleep(2) # Simulates an asynchronous wait time
36
+ return {"response": "Asynchronous AI response generated!"}
37
+
38
+ # Asynchronous OpenAI Query Endpoint
39
  @app.post("/query")
40
+ async def query_llm(request: QueryRequest):
41
+ """
42
+ Calls OpenAI's GPT-4 model asynchronously to generate a response.
43
+
44
+ Parameters:
45
+ request (QueryRequest): JSON containing a prompt for AI.
46
+
47
+ Returns:
48
+ dict: AI response as JSON.
49
+ """
50
+ response = await asyncio.to_thread(llm.predict, request.prompt) # Runs OpenAI call asynchronously
51
  return {"response": response}
52
 
53
+ # Run the FastAPI server on Hugging Face Spaces (Port 7860)
54
  if __name__ == "__main__":
55
  import uvicorn
56
+ uvicorn.run(app, host="0.0.0.0", port=7860)