MingDoan commited on
Commit
f26cbb9
·
1 Parent(s): cd70bd8

feat: Image to Text

Browse files
DOCS.md → .github/README.md RENAMED
@@ -72,13 +72,23 @@ This route checks the health status of children server. The children is consider
72
  ```json
73
  // Request. Form-data
74
  {
75
- "image": "<image-data"
76
  }
77
 
78
  // Response. JSON
79
  {
80
  "data": {
81
- "faces": []
 
 
 
 
 
 
 
 
 
 
82
  }
83
  }
84
  ```
@@ -125,6 +135,22 @@ This route checks the health status of children server. The children is consider
125
  }
126
  ```
127
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128
  ## 😊 Contributors
129
 
130
  - GDSC-FPTU [[gdsc-fptu](https://github.com/gdsc-fptu)]
 
72
  ```json
73
  // Request. Form-data
74
  {
75
+ "image": "<image-data>"
76
  }
77
 
78
  // Response. JSON
79
  {
80
  "data": {
81
+ "faces": FaceObject[]
82
+ }
83
+ }
84
+
85
+ // FaceObject
86
+ {
87
+ "bbox": {
88
+ "x": 0,
89
+ "y": 0,
90
+ "width": 100,
91
+ "height": 100
92
  }
93
  }
94
  ```
 
135
  }
136
  ```
137
 
138
+ - `/api/img2text/`
139
+
140
+ ```json
141
+ // Request. Form-data
142
+ {
143
+ "image": <image-data>
144
+ }
145
+
146
+ // Response. JSON
147
+ {
148
+ "data": {
149
+ "caption": "Caption of image"
150
+ }
151
+ }
152
+ ```
153
+
154
  ## 😊 Contributors
155
 
156
  - GDSC-FPTU [[gdsc-fptu](https://github.com/gdsc-fptu)]
Dockerfile CHANGED
@@ -2,7 +2,7 @@
2
  FROM python:3.11.7-slim-bookworm
3
 
4
  # Set environment variables
5
- ENV HOME=/home/user \
6
  PATH=/home/user/.local/bin:$PATH
7
 
8
  # Install libgl1-mesa-glx for opencv
@@ -16,7 +16,7 @@ RUN apt-get install 'ffmpeg'\
16
  RUN useradd -m -u 1000 user
17
 
18
  # Define working directory
19
- WORKDIR $HOME/app
20
 
21
  # Switch to user
22
  USER user
@@ -29,7 +29,7 @@ RUN pip install --no-cache-dir --upgrade pip
29
  RUN pip install --user -r /app/requirements.txt
30
 
31
  # Copy the rest of the code to the image
32
- COPY --chown=user:user . $HOME/app
33
 
34
  # Expose port 7860
35
  EXPOSE 7860/tcp
 
2
  FROM python:3.11.7-slim-bookworm
3
 
4
  # Set environment variables
5
+ ENV CLOUD_HOME=/home/user \
6
  PATH=/home/user/.local/bin:$PATH
7
 
8
  # Install libgl1-mesa-glx for opencv
 
16
  RUN useradd -m -u 1000 user
17
 
18
  # Define working directory
19
+ WORKDIR $CLOUD_HOME/app
20
 
21
  # Switch to user
22
  USER user
 
29
  RUN pip install --user -r /app/requirements.txt
30
 
31
  # Copy the rest of the code to the image
32
+ COPY --chown=user:user . $CLOUD_HOME/app
33
 
34
  # Expose port 7860
35
  EXPOSE 7860/tcp
apps/apis/chats/controllers/openai_controller.py CHANGED
@@ -7,10 +7,12 @@ BASE_ENHANCE_PROMPT = {
7
  "role": "system", "content": "You are a helpful assistant. You are helping a customer to solve a problem."}
8
 
9
 
10
- client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
11
 
12
 
13
- def send_message(prompt: str, histories: list = []):
 
 
14
  return client.chat.completions.create(
15
  model="gpt-3.5-turbo",
16
  messages=[
@@ -21,7 +23,9 @@ def send_message(prompt: str, histories: list = []):
21
  ).choices[0].message.content
22
 
23
 
24
- def send_message_conversation(conversation_id: str, prompt: str):
 
 
25
  # Update conversation
26
  update_conversation_storage(conversation_id, "user", prompt)
27
  # Generate response
 
7
  "role": "system", "content": "You are a helpful assistant. You are helping a customer to solve a problem."}
8
 
9
 
10
+ client = OpenAI()
11
 
12
 
13
+ def send_message(prompt: str, histories: list = [], api_key: str = None):
14
+ if api_key is not None:
15
+ client.api_key = api_key
16
  return client.chat.completions.create(
17
  model="gpt-3.5-turbo",
18
  messages=[
 
23
  ).choices[0].message.content
24
 
25
 
26
+ def send_message_conversation(conversation_id: str, prompt: str, api_key: str = None):
27
+ if api_key is not None:
28
+ client.api_key = api_key
29
  # Update conversation
30
  update_conversation_storage(conversation_id, "user", prompt)
31
  # Generate response
apps/apis/chats/models/chats_model.py CHANGED
@@ -5,3 +5,5 @@ class ChatsModel(BaseModel):
5
  prompt: str = Field(..., example="Hello, I'm a chatbot")
6
  histories: list[dict[str, str]] | None = Field(
7
  None, example=[{"user": "Hello", "assistant": "Hi"}])
 
 
 
5
  prompt: str = Field(..., example="Hello, I'm a chatbot")
6
  histories: list[dict[str, str]] | None = Field(
7
  None, example=[{"user": "Hello", "assistant": "Hi"}])
8
+ openai_key: str | None = Field(
9
+ None, descriptions="Open AI API key", example="OPENAI_KEY")
apps/apis/img2text/__init__.py CHANGED
@@ -1,5 +1,7 @@
1
- from fastapi import APIRouter
2
  from fastapi.responses import JSONResponse
 
 
3
 
4
  router = APIRouter(prefix='/img2text')
5
  router_base_configs = {
@@ -10,5 +12,16 @@ router_base_configs = {
10
 
11
  # Image to text
12
  @router.post("/", **router_base_configs)
13
- def image_to_text():
14
- return {"caption": "Hello World"}
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import APIRouter, Depends
2
  from fastapi.responses import JSONResponse
3
+ from apps.services.foward.fw_middleware import forward_middleware, forward_request
4
+ from .models.img2text_model import Img2TextModel
5
 
6
  router = APIRouter(prefix='/img2text')
7
  router_base_configs = {
 
12
 
13
  # Image to text
14
  @router.post("/", **router_base_configs)
15
+ def image_to_text(
16
+ image: Img2TextModel.image = Img2TextModel.image_default,
17
+ fw_index: Img2TextModel.fw_index = Depends(forward_middleware)
18
+ ):
19
+ # Forward request
20
+ fw_data = {
21
+ "files": {"image": image.file}
22
+ }
23
+ fw_response = forward_request(fw_index, fw_data, '/api/img2text/')
24
+ if fw_response is not None:
25
+ return {"caption": fw_response["data"]["caption"]}
26
+
27
+ return {"caption": "Image to Text model is not available in this server"}
apps/apis/img2text/models/img2text_model.py ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ from fastapi import File, UploadFile
2
+
3
+
4
+ class Img2TextModel:
5
+ image = UploadFile | None
6
+ image_default = File(None, description="Image file")
7
+ fw_index = int | None