Spaces:
Runtime error
Runtime error
refactor to support dockerization
Browse files- Dockerfile +23 -0
- crew/til.py +24 -7
- endpoints.py +25 -8
- requirements.txt +3 -0
- test.py +1 -1
Dockerfile
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# # https://hub.docker.com/_/python
|
| 2 |
+
# FROM python:3.11-slim-bullseye
|
| 3 |
+
|
| 4 |
+
# ENV PYTHONUNBUFFERED True
|
| 5 |
+
# ENV APP_HOME /app
|
| 6 |
+
# WORKDIR $APP_HOME
|
| 7 |
+
# COPY . ./
|
| 8 |
+
|
| 9 |
+
# RUN pip install --no-cache-dir -r requirements.txt
|
| 10 |
+
|
| 11 |
+
# CMD ["uvicorn", "endpoints:app", "--host", "0.0.0.0", "--port", "8000"]
|
| 12 |
+
|
| 13 |
+
FROM python:3.11
|
| 14 |
+
|
| 15 |
+
WORKDIR /code
|
| 16 |
+
|
| 17 |
+
COPY ./requirements.txt /code/requirements.txt
|
| 18 |
+
|
| 19 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
| 20 |
+
|
| 21 |
+
COPY . ./
|
| 22 |
+
|
| 23 |
+
CMD ["fastapi", "run", "endpoints.py", "--port", "80"]
|
crew/til.py
CHANGED
|
@@ -1,19 +1,20 @@
|
|
| 1 |
from langchain_core.prompts import ChatPromptTemplate, HumanMessagePromptTemplate
|
| 2 |
from langchain_core.messages import SystemMessage
|
| 3 |
-
from pydantic import BaseModel, Field
|
| 4 |
from langchain_core.output_parsers import JsonOutputParser
|
| 5 |
from langchain_openai import ChatOpenAI
|
| 6 |
-
from
|
|
|
|
| 7 |
import pprint
|
| 8 |
import os
|
| 9 |
|
| 10 |
HIGH_IMPACT_THRESHOLD = 8
|
| 11 |
LOW_IMPACT_THRESHOLD = 7
|
| 12 |
-
# OPENAI_MODEL = "gpt-4o"
|
| 13 |
-
# OPENAI_MODEL = "gpt-3.5-turbo"
|
| 14 |
|
| 15 |
class TilCrew:
|
| 16 |
def kickoff(self, inputs={}):
|
|
|
|
|
|
|
| 17 |
self.content = inputs["content"]
|
| 18 |
self._gather_feedback()
|
| 19 |
return self._final_call_on_feedback()
|
|
@@ -56,14 +57,19 @@ class TilCrew:
|
|
| 56 |
result["feedback"] = "ok"
|
| 57 |
final_results = final_results + [result]
|
| 58 |
|
|
|
|
| 59 |
print("Final Results:")
|
| 60 |
-
pprint.pp(
|
| 61 |
-
return
|
| 62 |
|
| 63 |
def _gather_feedback(self):
|
| 64 |
feedback_chain = self._build_feedback_chain()
|
| 65 |
pprint.pp("Analysing the TIL.....")
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
print("Feedback: ")
|
| 68 |
pprint.pp(self.feedback_results)
|
| 69 |
|
|
@@ -120,3 +126,14 @@ class TilFeedbackResult(BaseModel):
|
|
| 120 |
|
| 121 |
class TilFeedbackResults(BaseModel):
|
| 122 |
tils: List[TilFeedbackResult]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from langchain_core.prompts import ChatPromptTemplate, HumanMessagePromptTemplate
|
| 2 |
from langchain_core.messages import SystemMessage
|
| 3 |
+
from pydantic import BaseModel, Field, UUID4
|
| 4 |
from langchain_core.output_parsers import JsonOutputParser
|
| 5 |
from langchain_openai import ChatOpenAI
|
| 6 |
+
from langchain import callbacks
|
| 7 |
+
from typing import List, Optional
|
| 8 |
import pprint
|
| 9 |
import os
|
| 10 |
|
| 11 |
HIGH_IMPACT_THRESHOLD = 8
|
| 12 |
LOW_IMPACT_THRESHOLD = 7
|
|
|
|
|
|
|
| 13 |
|
| 14 |
class TilCrew:
|
| 15 |
def kickoff(self, inputs={}):
|
| 16 |
+
print("Human Message:")
|
| 17 |
+
pprint.pp(inputs)
|
| 18 |
self.content = inputs["content"]
|
| 19 |
self._gather_feedback()
|
| 20 |
return self._final_call_on_feedback()
|
|
|
|
| 57 |
result["feedback"] = "ok"
|
| 58 |
final_results = final_results + [result]
|
| 59 |
|
| 60 |
+
response = {"feedback": final_results, "run_id": self.run_id }
|
| 61 |
print("Final Results:")
|
| 62 |
+
pprint.pp(response)
|
| 63 |
+
return response
|
| 64 |
|
| 65 |
def _gather_feedback(self):
|
| 66 |
feedback_chain = self._build_feedback_chain()
|
| 67 |
pprint.pp("Analysing the TIL.....")
|
| 68 |
+
with callbacks.collect_runs() as cb:
|
| 69 |
+
self.feedback_results = feedback_chain.invoke({"til_content": self.content})['tils']
|
| 70 |
+
self.run_id = cb.traced_runs[0].id
|
| 71 |
+
print("Run ID: ", self.run_id)
|
| 72 |
+
|
| 73 |
print("Feedback: ")
|
| 74 |
pprint.pp(self.feedback_results)
|
| 75 |
|
|
|
|
| 126 |
|
| 127 |
class TilFeedbackResults(BaseModel):
|
| 128 |
tils: List[TilFeedbackResult]
|
| 129 |
+
|
| 130 |
+
class TilFinalFeedback(BaseModel):
|
| 131 |
+
til: str
|
| 132 |
+
feedback: str
|
| 133 |
+
feedback_criteria: Optional[str] = None
|
| 134 |
+
reason: Optional[str] = None
|
| 135 |
+
suggestion: Optional[str] = None
|
| 136 |
+
|
| 137 |
+
class TilFeedbackResponse(BaseModel):
|
| 138 |
+
run_id: UUID4
|
| 139 |
+
feedback: List[TilFinalFeedback]
|
endpoints.py
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
|
|
| 1 |
import uvicorn
|
| 2 |
from fastapi import FastAPI, Query
|
| 3 |
from crew import til
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
description = """
|
| 6 |
API helps you do awesome stuff. 🚀
|
|
@@ -9,26 +13,39 @@ description = """
|
|
| 9 |
|
| 10 |
tags_metadata = [
|
| 11 |
{
|
| 12 |
-
"name": "
|
| 13 |
-
"description": "Gives the feedback
|
| 14 |
},
|
| 15 |
]
|
| 16 |
|
| 17 |
app = FastAPI(
|
| 18 |
-
title="Growthy
|
| 19 |
description=description,
|
| 20 |
summary="Deadpool's favorite app. Nuff said.",
|
| 21 |
version="0.0.1",
|
| 22 |
openapi_tags=tags_metadata,
|
| 23 |
docs_url="/documentation",
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
result = til.TilCrew().kickoff(inputs)
|
| 30 |
return result
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
if __name__ == "__main__":
|
| 33 |
uvicorn.run(app, host="127.0.0.1", port=8000)
|
| 34 |
|
|
|
|
| 1 |
+
from dotenv import load_dotenv
|
| 2 |
import uvicorn
|
| 3 |
from fastapi import FastAPI, Query
|
| 4 |
from crew import til
|
| 5 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
+
from typing import List
|
| 7 |
+
load_dotenv()
|
| 8 |
|
| 9 |
description = """
|
| 10 |
API helps you do awesome stuff. 🚀
|
|
|
|
| 13 |
|
| 14 |
tags_metadata = [
|
| 15 |
{
|
| 16 |
+
"name": "til_feedback",
|
| 17 |
+
"description": "Gives the feedback on user's TIL content",
|
| 18 |
},
|
| 19 |
]
|
| 20 |
|
| 21 |
app = FastAPI(
|
| 22 |
+
title="Growthy AI Worflows",
|
| 23 |
description=description,
|
| 24 |
summary="Deadpool's favorite app. Nuff said.",
|
| 25 |
version="0.0.1",
|
| 26 |
openapi_tags=tags_metadata,
|
| 27 |
docs_url="/documentation",
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
app.add_middleware(
|
| 31 |
+
CORSMiddleware,
|
| 32 |
+
allow_origins=["*"],
|
| 33 |
+
allow_credentials=True,
|
| 34 |
+
allow_methods=["*"],
|
| 35 |
+
allow_headers=["*"],
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
@app.get("/til_feedback", tags=["til_feedback"])
|
| 39 |
+
async def til_feedback_kickoff(content: List[str] = Query(...)) -> til.TilFeedbackResponse:
|
| 40 |
+
separator = "\n"
|
| 41 |
+
inputs = {"content": separator.join(content)}
|
| 42 |
result = til.TilCrew().kickoff(inputs)
|
| 43 |
return result
|
| 44 |
|
| 45 |
+
@app.get("/healthcheck")
|
| 46 |
+
async def read_root():
|
| 47 |
+
return {"status": "ok"}
|
| 48 |
+
|
| 49 |
if __name__ == "__main__":
|
| 50 |
uvicorn.run(app, host="127.0.0.1", port=8000)
|
| 51 |
|
requirements.txt
CHANGED
|
@@ -9,3 +9,6 @@ tavily-python
|
|
| 9 |
arxiv
|
| 10 |
semanticscholar
|
| 11 |
streamlit-extras
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
arxiv
|
| 10 |
semanticscholar
|
| 11 |
streamlit-extras
|
| 12 |
+
fastapi
|
| 13 |
+
uvicorn
|
| 14 |
+
fastapi_cors
|
test.py
CHANGED
|
@@ -41,7 +41,7 @@ def main():
|
|
| 41 |
with stdout(log_container.code, terminator=""):
|
| 42 |
feedback = TilCrew()
|
| 43 |
inputs = {"content": til_content}
|
| 44 |
-
results = feedback.kickoff(inputs=inputs)
|
| 45 |
status.update(
|
| 46 |
label="✅ Feedback ready!",
|
| 47 |
state="complete",
|
|
|
|
| 41 |
with stdout(log_container.code, terminator=""):
|
| 42 |
feedback = TilCrew()
|
| 43 |
inputs = {"content": til_content}
|
| 44 |
+
results = feedback.kickoff(inputs=inputs)["feedback"]
|
| 45 |
status.update(
|
| 46 |
label="✅ Feedback ready!",
|
| 47 |
state="complete",
|