Spaces:
Sleeping
Sleeping
Lazar Radojevic
commited on
Commit
·
0b497e7
1
Parent(s):
da82b2b
add poe tasks
Browse files- README.md +10 -2
- backend/routes.py +2 -3
- frontend/app_ui.py +1 -2
- poe/common-tasks.toml +14 -2
- run.py +2 -2
- tests/test_load_data.py +3 -4
- tests/test_similar_prompts.py +4 -3
README.md
CHANGED
|
@@ -59,17 +59,25 @@ The backend API provides the following endpoint:
|
|
| 59 |
The repository includes a Dockerfile to build and run the backend service. To build the image:
|
| 60 |
|
| 61 |
```bash
|
| 62 |
-
|
| 63 |
```
|
| 64 |
|
| 65 |
To start the backend service, run:
|
| 66 |
|
| 67 |
```bash
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
```
|
| 70 |
|
| 71 |
This command will build the Docker image and start the service.
|
| 72 |
|
|
|
|
|
|
|
| 73 |
### Deployment
|
| 74 |
|
| 75 |
The project is integrated with HuggingFace Spaces. The solution is automatically deployed to HuggingFace with every push to the repository.
|
|
|
|
| 59 |
The repository includes a Dockerfile to build and run the backend service. To build the image:
|
| 60 |
|
| 61 |
```bash
|
| 62 |
+
poe build
|
| 63 |
```
|
| 64 |
|
| 65 |
To start the backend service, run:
|
| 66 |
|
| 67 |
```bash
|
| 68 |
+
poe start-backend
|
| 69 |
+
```
|
| 70 |
+
|
| 71 |
+
To run both use:
|
| 72 |
+
|
| 73 |
+
```bash
|
| 74 |
+
poe backend
|
| 75 |
```
|
| 76 |
|
| 77 |
This command will build the Docker image and start the service.
|
| 78 |
|
| 79 |
+
Note: It takes some time for this service to start.
|
| 80 |
+
|
| 81 |
### Deployment
|
| 82 |
|
| 83 |
The project is integrated with HuggingFace Spaces. The solution is automatically deployed to HuggingFace with every push to the repository.
|
backend/routes.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
|
|
|
|
| 3 |
from fastapi import APIRouter, HTTPException
|
| 4 |
from fastapi.responses import HTMLResponse
|
| 5 |
|
|
@@ -7,8 +8,6 @@ from backend.models import QueryRequest, QueryResponse, SimilarPrompt
|
|
| 7 |
from src.prompt_loader import PromptLoader
|
| 8 |
from src.search_engine import PromptSearchEngine
|
| 9 |
|
| 10 |
-
from dotenv import load_dotenv
|
| 11 |
-
|
| 12 |
# Load environment variables from .env file
|
| 13 |
load_dotenv()
|
| 14 |
|
|
@@ -82,7 +81,7 @@ async def home_page() -> HTMLResponse:
|
|
| 82 |
<h1>Prompt Search Engine API</h1>
|
| 83 |
<p>Use this API to find similar prompts based on a query.</p>
|
| 84 |
<h2>POST /most_similar</h2>
|
| 85 |
-
<p><strong>Request:</strong> <code>{"query": "string", "n":
|
| 86 |
<p><strong>Response:</strong> <code>{"similar_prompts": [{"score": 0.95, "prompt": "Example prompt 1"}]}</code></p>
|
| 87 |
</div>
|
| 88 |
</body>
|
|
|
|
| 1 |
import os
|
| 2 |
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
from fastapi import APIRouter, HTTPException
|
| 5 |
from fastapi.responses import HTMLResponse
|
| 6 |
|
|
|
|
| 8 |
from src.prompt_loader import PromptLoader
|
| 9 |
from src.search_engine import PromptSearchEngine
|
| 10 |
|
|
|
|
|
|
|
| 11 |
# Load environment variables from .env file
|
| 12 |
load_dotenv()
|
| 13 |
|
|
|
|
| 81 |
<h1>Prompt Search Engine API</h1>
|
| 82 |
<p>Use this API to find similar prompts based on a query.</p>
|
| 83 |
<h2>POST /most_similar</h2>
|
| 84 |
+
<p><strong>Request:</strong> <code>{"query": "string", "n": 1}</code></p>
|
| 85 |
<p><strong>Response:</strong> <code>{"similar_prompts": [{"score": 0.95, "prompt": "Example prompt 1"}]}</code></p>
|
| 86 |
</div>
|
| 87 |
</body>
|
frontend/app_ui.py
CHANGED
|
@@ -2,7 +2,6 @@ import os
|
|
| 2 |
|
| 3 |
import requests
|
| 4 |
import streamlit as st
|
| 5 |
-
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
|
| 8 |
# Load environment variables from .env file
|
|
@@ -34,7 +33,7 @@ def get_similar_prompts(query: str, n: int) -> dict:
|
|
| 34 |
return response.json()
|
| 35 |
except requests.RequestException as e:
|
| 36 |
st.error(f"Error: {e}")
|
| 37 |
-
return
|
| 38 |
|
| 39 |
|
| 40 |
def get_color(score: float) -> str:
|
|
|
|
| 2 |
|
| 3 |
import requests
|
| 4 |
import streamlit as st
|
|
|
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
|
| 7 |
# Load environment variables from .env file
|
|
|
|
| 33 |
return response.json()
|
| 34 |
except requests.RequestException as e:
|
| 35 |
st.error(f"Error: {e}")
|
| 36 |
+
return {}
|
| 37 |
|
| 38 |
|
| 39 |
def get_color(score: float) -> str:
|
poe/common-tasks.toml
CHANGED
|
@@ -26,7 +26,7 @@ sequence = ["style-isort", "style-black"]
|
|
| 26 |
|
| 27 |
[tool.poe.tasks.types]
|
| 28 |
help = "Run the type checker"
|
| 29 |
-
cmd = "mypy . --ignore-missing-imports --check-untyped-defs --install-types --non-interactive"
|
| 30 |
|
| 31 |
[tool.poe.tasks.lint]
|
| 32 |
help = "Evaluate ruff rules"
|
|
@@ -53,4 +53,16 @@ sequence = ["style", "types", "lint", "clean"]
|
|
| 53 |
|
| 54 |
[tool.poe.tasks.frontend]
|
| 55 |
help = "Start the UI"
|
| 56 |
-
cmd = "streamlit run ./frontend/app_ui.py"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
[tool.poe.tasks.types]
|
| 28 |
help = "Run the type checker"
|
| 29 |
+
cmd = "mypy . --ignore-missing-imports --check-untyped-defs --install-types --non-interactive --explicit-package-bases"
|
| 30 |
|
| 31 |
[tool.poe.tasks.lint]
|
| 32 |
help = "Evaluate ruff rules"
|
|
|
|
| 53 |
|
| 54 |
[tool.poe.tasks.frontend]
|
| 55 |
help = "Start the UI"
|
| 56 |
+
cmd = "streamlit run ./frontend/app_ui.py"
|
| 57 |
+
|
| 58 |
+
[tool.poe.tasks.build]
|
| 59 |
+
help = "Build the Docker image"
|
| 60 |
+
cmd = "docker build -t prompt-search-engine ."
|
| 61 |
+
|
| 62 |
+
[tool.poe.tasks.start-backend]
|
| 63 |
+
help = "Start the backend"
|
| 64 |
+
cmd = "docker run -d -p 8000:8000 prompt-search-engine"
|
| 65 |
+
|
| 66 |
+
[tool.poe.tasks.backend]
|
| 67 |
+
help = "Start the backend"
|
| 68 |
+
sequence = ["build", "start-backend"]
|
run.py
CHANGED
|
@@ -3,10 +3,10 @@ import uvicorn
|
|
| 3 |
|
| 4 |
def run_fastapi_app():
|
| 5 |
uvicorn.run(
|
| 6 |
-
"backend.main:app",
|
| 7 |
host="0.0.0.0",
|
| 8 |
port=8000,
|
| 9 |
-
reload=True,
|
| 10 |
)
|
| 11 |
|
| 12 |
|
|
|
|
| 3 |
|
| 4 |
def run_fastapi_app():
|
| 5 |
uvicorn.run(
|
| 6 |
+
"backend.main:app",
|
| 7 |
host="0.0.0.0",
|
| 8 |
port=8000,
|
| 9 |
+
reload=True,
|
| 10 |
)
|
| 11 |
|
| 12 |
|
tests/test_load_data.py
CHANGED
|
@@ -1,8 +1,7 @@
|
|
| 1 |
import unittest
|
| 2 |
-
from unittest.mock import
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
)
|
| 6 |
|
| 7 |
|
| 8 |
class TestPromptLoader(unittest.TestCase):
|
|
|
|
| 1 |
import unittest
|
| 2 |
+
from unittest.mock import MagicMock, patch
|
| 3 |
+
|
| 4 |
+
from src.prompt_loader import PromptLoader
|
|
|
|
| 5 |
|
| 6 |
|
| 7 |
class TestPromptLoader(unittest.TestCase):
|
tests/test_similar_prompts.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
import unittest
|
| 2 |
-
from unittest.mock import
|
|
|
|
| 3 |
import requests
|
| 4 |
|
| 5 |
-
# Assuming the function to be tested is in a module named `frontend.app_ui`
|
| 6 |
from frontend.app_ui import get_similar_prompts
|
| 7 |
|
| 8 |
|
|
@@ -32,7 +32,8 @@ class TestGetSimilarPrompts(unittest.TestCase):
|
|
| 32 |
result = get_similar_prompts("test query", 3)
|
| 33 |
|
| 34 |
# Assertions
|
| 35 |
-
self.
|
|
|
|
| 36 |
|
| 37 |
|
| 38 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import unittest
|
| 2 |
+
from unittest.mock import Mock, patch
|
| 3 |
+
|
| 4 |
import requests
|
| 5 |
|
|
|
|
| 6 |
from frontend.app_ui import get_similar_prompts
|
| 7 |
|
| 8 |
|
|
|
|
| 32 |
result = get_similar_prompts("test query", 3)
|
| 33 |
|
| 34 |
# Assertions
|
| 35 |
+
self.assertIsInstance(result, dict)
|
| 36 |
+
self.assertEqual(len(result), 0)
|
| 37 |
|
| 38 |
|
| 39 |
if __name__ == "__main__":
|