Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,66 +1,57 @@
|
|
1 |
-
from fastapi import FastAPI
|
2 |
-
from pydantic import BaseModel
|
3 |
-
from fastapi.middleware.cors import CORSMiddleware
|
4 |
-
import uvicorn
|
5 |
-
from langchain_ollama import OllamaLLM
|
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 Chrome extensions
|
13 |
-
allow_credentials=True,
|
14 |
-
allow_methods=["*"],
|
15 |
-
allow_headers=["*"],
|
16 |
-
)
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
)
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
text = request.text
|
59 |
-
# Generate meaning using the LLM call
|
60 |
-
meaning = get_meaning_from_llm(text)
|
61 |
-
# Return the meaning in a JSON response
|
62 |
-
return MeaningResponse(meaning=meaning)
|
63 |
-
|
64 |
-
if __name__ == "__main__":
|
65 |
-
# Run the FastAPI app with Uvicorn
|
66 |
-
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
from pydantic import BaseModel
|
3 |
+
from fastapi.middleware.cors import CORSMiddleware
|
4 |
+
import uvicorn
|
5 |
+
from langchain_ollama import OllamaLLM
|
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 Chrome extensions
|
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
|
21 |
+
|
22 |
+
# Define the response model that will return the meaning
|
23 |
+
class MeaningResponse(BaseModel):
|
24 |
+
meaning: str
|
25 |
+
|
26 |
+
def get_meaning_from_llm(text: str) -> str:
|
27 |
+
"""
|
28 |
+
Get meaning of text using Ollama LLM.
|
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 = OllamaLLM(
|
35 |
+
model="llama3.2",
|
36 |
+
base_url="https://earwig-exact-slug.ngrok-free.app",
|
37 |
+
temperature=0.25
|
38 |
+
)
|
39 |
+
meaning = llm(prompt)
|
40 |
+
return meaning
|
41 |
+
|
42 |
+
@app.post("/get_meaning", response_model=MeaningResponse)
|
43 |
+
async def get_meaning(request: MeaningRequest):
|
44 |
+
"""
|
45 |
+
Endpoint to receive text and return its 'meaning' as generated by an LLM.
|
46 |
+
"""
|
47 |
+
print(f"Received text: {request.text}")
|
48 |
+
# Extract text from the request
|
49 |
+
text = request.text
|
50 |
+
# Generate meaning using the LLM call
|
51 |
+
meaning = get_meaning_from_llm(text)
|
52 |
+
# Return the meaning in a JSON response
|
53 |
+
return MeaningResponse(meaning=meaning)
|
54 |
+
|
55 |
+
if __name__ == "__main__":
|
56 |
+
# Run the FastAPI app with Uvicorn
|
57 |
+
uvicorn.run("app:app", host="0.0.0.0", port=8000, reload=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|