engasm89 commited on
Commit
d16ecdc
·
verified ·
1 Parent(s): f42af13

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Install required libraries
2
+ !pip install fastapi uvicorn pyngrok nest-asyncio langchain langchain-community langchain-openai
3
+
4
+ # Import necessary libraries
5
+ from fastapi import FastAPI
6
+ from langchain_openai import ChatOpenAI
7
+ from pydantic import BaseModel
8
+ import uvicorn
9
+ import nest_asyncio
10
+ from pyngrok import ngrok, conf
11
+
12
+ # Apply asyncio patch to run FastAPI inside Colab
13
+ nest_asyncio.apply()
14
+
15
+ # Initialize FastAPI application
16
+ app = FastAPI()
17
+
18
+ # Initialize OpenAI language model (Set your API key)
19
+ llm = ChatOpenAI(model_name="gpt-4", openai_api_key="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") # Replace with your actual API key
20
+
21
+ # Define a request model for structured API input
22
+ class QueryRequest(BaseModel):
23
+ prompt: str
24
+
25
+ # Define an endpoint to handle AI queries
26
+ @app.post("/query")
27
+ def query_llm(request: QueryRequest):
28
+ """
29
+ Handles user queries and returns AI-generated responses.
30
+
31
+ Parameters:
32
+ request (QueryRequest): JSON payload containing the prompt.
33
+
34
+ Returns:
35
+ dict: AI response as JSON.
36
+ """
37
+ response = llm.predict(request.prompt) # Call OpenAI model to generate response
38
+ return {"response": response} # Return response as JSON
39
+
40
+ # Set up ngrok to expose the FastAPI app
41
+ # Set your authtoken here
42
+ conf.get_default().auth_token = "2tTyxCxX3Ckk5HKetSNlNBqTepQ_6mss4xgMTbfUC5E6esgvU" # Replace with your actual authtoken
43
+ public_url = ngrok.connect(8000).public_url
44
+ print(f"Public API URL: {public_url}")
45
+
46
+ # Run FastAPI using Uvicorn inside Colab
47
+ uvicorn.run(app, host="0.0.0.0", port=8000)