Update app.py
Browse files
app.py
CHANGED
@@ -1,20 +1,28 @@
|
|
1 |
-
from fastapi import FastAPI
|
2 |
from pydantic import BaseModel
|
3 |
from fastapi.middleware.cors import CORSMiddleware
|
4 |
import uvicorn
|
5 |
-
from
|
|
|
|
|
|
|
|
|
6 |
|
7 |
app = FastAPI()
|
8 |
|
|
|
9 |
# Allow requests from your front-end's origin.
|
10 |
app.add_middleware(
|
11 |
CORSMiddleware,
|
12 |
-
allow_origins=["chrome-extension://*"], # Allow
|
13 |
allow_credentials=True,
|
14 |
allow_methods=["*"],
|
15 |
allow_headers=["*"],
|
16 |
)
|
17 |
|
|
|
|
|
|
|
18 |
# Define the request model that expects a JSON body with "text"
|
19 |
class MeaningRequest(BaseModel):
|
20 |
text: str
|
@@ -25,33 +33,42 @@ class MeaningResponse(BaseModel):
|
|
25 |
|
26 |
def get_meaning_from_llm(text: str) -> str:
|
27 |
"""
|
28 |
-
Get meaning of text using
|
29 |
"""
|
30 |
# Create a prompt for your LLM
|
31 |
prompt = f"Explain the meaning of the following text in simple terms in only one or two lines not more than that: '{text}'"
|
32 |
|
33 |
# Make sure this URL is accessible and valid
|
34 |
-
llm =
|
35 |
-
model="
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
38 |
)
|
39 |
-
|
40 |
-
return
|
41 |
|
42 |
@app.post("/get_meaning", response_model=MeaningResponse)
|
43 |
async def get_meaning(request: MeaningRequest):
|
44 |
"""
|
45 |
-
Endpoint to
|
46 |
"""
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
if __name__ == "__main__":
|
56 |
# Run the FastAPI app with Uvicorn
|
57 |
-
uvicorn.run("
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
from pydantic import BaseModel
|
3 |
from fastapi.middleware.cors import CORSMiddleware
|
4 |
import uvicorn
|
5 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
6 |
+
import os
|
7 |
+
from dotenv import load_dotenv
|
8 |
+
|
9 |
+
load_dotenv()
|
10 |
|
11 |
app = FastAPI()
|
12 |
|
13 |
+
|
14 |
# Allow requests from your front-end's origin.
|
15 |
app.add_middleware(
|
16 |
CORSMiddleware,
|
17 |
+
allow_origins=["http://localhost:3000", "chrome-extension://*"], # Allow specific origins
|
18 |
allow_credentials=True,
|
19 |
allow_methods=["*"],
|
20 |
allow_headers=["*"],
|
21 |
)
|
22 |
|
23 |
+
# It's recommended to load secrets from environment variables
|
24 |
+
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
25 |
+
|
26 |
# Define the request model that expects a JSON body with "text"
|
27 |
class MeaningRequest(BaseModel):
|
28 |
text: str
|
|
|
33 |
|
34 |
def get_meaning_from_llm(text: str) -> str:
|
35 |
"""
|
36 |
+
Get meaning of text using Google's Generative AI.
|
37 |
"""
|
38 |
# Create a prompt for your LLM
|
39 |
prompt = f"Explain the meaning of the following text in simple terms in only one or two lines not more than that: '{text}'"
|
40 |
|
41 |
# Make sure this URL is accessible and valid
|
42 |
+
llm = ChatGoogleGenerativeAI(
|
43 |
+
model="gemini-1.5-flash",
|
44 |
+
temperature=0.1,
|
45 |
+
max_tokens=None,
|
46 |
+
timeout=None,
|
47 |
+
max_retries=2,
|
48 |
+
google_api_key=GOOGLE_API_KEY
|
49 |
)
|
50 |
+
response = llm.invoke(prompt)
|
51 |
+
return response.content
|
52 |
|
53 |
@app.post("/get_meaning", response_model=MeaningResponse)
|
54 |
async def get_meaning(request: MeaningRequest):
|
55 |
"""
|
56 |
+
Endpoint to return meaning.
|
57 |
"""
|
58 |
+
try:
|
59 |
+
print(f"Received text: {request.text}")
|
60 |
+
# Extract text from the request
|
61 |
+
text = request.text
|
62 |
+
# Generate meaning using the LLM call
|
63 |
+
meaning = get_meaning_from_llm(text)
|
64 |
+
|
65 |
+
return MeaningResponse(
|
66 |
+
meaning=meaning
|
67 |
+
)
|
68 |
+
except Exception as e:
|
69 |
+
print(f"An error occurred: {e}")
|
70 |
+
raise HTTPException(status_code=500, detail=str(e))
|
71 |
|
72 |
if __name__ == "__main__":
|
73 |
# Run the FastAPI app with Uvicorn
|
74 |
+
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)
|