Spaces:
Sleeping
Sleeping
Lazar Radojevic
commited on
Commit
Β·
7af929b
1
Parent(s):
7724373
different structure
Browse files- .gitattributes +0 -35
- .gitignore +1 -0
- Dockerfile +17 -27
- README.md +39 -12
- backend/Dockerfile +23 -0
- __init__.py β backend/__init__.py +0 -0
- backend/poe/common-tasks.toml +52 -0
- backend/poetry.lock +0 -0
- backend/pyproject.toml +27 -0
- backend/run.py +49 -0
- {src β backend/src}/__init__.py +0 -0
- {src β backend/src}/prompt_loader.py +0 -0
- {src β backend/src}/search_engine.py +0 -0
- {src β backend/src}/similarity_scorer.py +0 -0
- {src β backend/src}/vectorizer.py +0 -0
- docker-compose.yml +16 -0
- frontend/Dockerfile +22 -0
- frontend/poe/common-tasks.toml +52 -0
- frontend/poetry.lock +0 -0
- frontend/pyproject.toml +23 -0
- frontend/run.py +37 -0
- main.py +0 -27
- requirements.txt +0 -8
- run.py +0 -4
.gitattributes
DELETED
@@ -1,35 +0,0 @@
|
|
1 |
-
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
-
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
-
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
-
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
-
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
-
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
-
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
-
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
-
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
-
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
-
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
-
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
-
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
-
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
-
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
-
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
-
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
-
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
-
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
-
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
-
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
-
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
-
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
-
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
-
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
-
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
-
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
-
*.tar filter=lfs diff=lfs merge=lfs -text
|
29 |
-
*.tflite filter=lfs diff=lfs merge=lfs -text
|
30 |
-
*.tgz filter=lfs diff=lfs merge=lfs -text
|
31 |
-
*.wasm filter=lfs diff=lfs merge=lfs -text
|
32 |
-
*.xz filter=lfs diff=lfs merge=lfs -text
|
33 |
-
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
-
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
-
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
__pycache__
|
Dockerfile
CHANGED
@@ -1,33 +1,23 @@
|
|
1 |
-
# Use the official
|
2 |
-
FROM
|
3 |
|
4 |
-
#
|
5 |
-
|
6 |
-
|
|
|
7 |
|
8 |
-
# Set
|
9 |
-
|
10 |
|
11 |
-
#
|
12 |
-
|
13 |
|
14 |
-
#
|
15 |
-
|
|
|
16 |
|
17 |
-
#
|
18 |
-
|
19 |
|
20 |
-
#
|
21 |
-
|
22 |
-
|
23 |
-
# Install the application dependencies
|
24 |
-
RUN pip install --no-cache-dir -r requirements.txt
|
25 |
-
|
26 |
-
# Copy the rest of the application code to the working directory
|
27 |
-
COPY --chown=user . .
|
28 |
-
|
29 |
-
# Expose the port FastAPI will run on
|
30 |
-
EXPOSE 7860
|
31 |
-
|
32 |
-
# Command to run the FastAPI application
|
33 |
-
CMD ["python", "run.py"]
|
|
|
1 |
+
# Use the official Docker image with Docker and Docker Compose
|
2 |
+
FROM docker:latest
|
3 |
|
4 |
+
# Install Docker Compose
|
5 |
+
RUN apk add --no-cache \
|
6 |
+
py3-pip \
|
7 |
+
&& pip install docker-compose
|
8 |
|
9 |
+
# Set the working directory to /app
|
10 |
+
WORKDIR /app
|
11 |
|
12 |
+
# Copy the docker-compose.yml file into the container at /app
|
13 |
+
COPY docker-compose.yml .
|
14 |
|
15 |
+
# Copy the frontend and backend directories into the container
|
16 |
+
COPY frontend ./frontend
|
17 |
+
COPY backend ./backend
|
18 |
|
19 |
+
# Expose ports (adjust if necessary)
|
20 |
+
EXPOSE 8501 8080
|
21 |
|
22 |
+
# Run docker-compose up when the container starts
|
23 |
+
CMD ["docker-compose", "up"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
README.md
CHANGED
@@ -1,12 +1,39 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
### Installation
|
2 |
+
|
3 |
+
This project uses [poetry](https://python-poetry.org/) as dependency management tool.
|
4 |
+
|
5 |
+
After setting up poetry, run the following command to install the project along with its dependencies:
|
6 |
+
|
7 |
+
```bash
|
8 |
+
poetry install
|
9 |
+
```
|
10 |
+
|
11 |
+
The project also uses of [poethepoet](https://poethepoet.natn.io/index.html) for defining project tasks, such as running local applications for development.
|
12 |
+
|
13 |
+
These "_poe tasks_" are defined in the `pyproject.toml` file.
|
14 |
+
|
15 |
+
### Code quality and typing checks
|
16 |
+
|
17 |
+
This project enforces code quality and typing checks using [ruff](https://docs.astral.sh/ruff/), [mypy](https://mypy.readthedocs.io/en/stable/) and [black](https://black.readthedocs.io/en/stable/).
|
18 |
+
|
19 |
+
To format the code, run the following command:
|
20 |
+
|
21 |
+
```bash
|
22 |
+
poetry run poe format
|
23 |
+
```
|
24 |
+
|
25 |
+
To run the checks locally, run the following command:
|
26 |
+
|
27 |
+
```bash
|
28 |
+
poetry run poe check
|
29 |
+
```
|
30 |
+
|
31 |
+
You can also use poe task to run the tests, namely:
|
32 |
+
|
33 |
+
```bash
|
34 |
+
poetry run poe test
|
35 |
+
```
|
36 |
+
|
37 |
+
docker build -t prompt-search-engine .
|
38 |
+
|
39 |
+
docker run -d -p 8000:8000 prompt-search-engine
|
backend/Dockerfile
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Use the official Python image from the Docker Hub
|
2 |
+
FROM python:3.10-slim
|
3 |
+
|
4 |
+
# Set the working directory in the container
|
5 |
+
WORKDIR /app
|
6 |
+
|
7 |
+
# Install Poetry
|
8 |
+
RUN pip install poetry
|
9 |
+
|
10 |
+
# Copy only the pyproject.toml and poetry.lock files to install dependencies first
|
11 |
+
COPY pyproject.toml poetry.lock ./
|
12 |
+
|
13 |
+
# Install dependencies using Poetry
|
14 |
+
RUN poetry config virtualenvs.create false && poetry install --only=main
|
15 |
+
|
16 |
+
# Copy the rest of the application code to the working directory
|
17 |
+
COPY . .
|
18 |
+
|
19 |
+
# Expose the port FastAPI will run on
|
20 |
+
EXPOSE 8000
|
21 |
+
|
22 |
+
# Command to run the FastAPI application
|
23 |
+
CMD ["poetry", "run", "uvicorn", "run:app", "--host", "0.0.0.0", "--port", "8000", "--reload"]
|
__init__.py β backend/__init__.py
RENAMED
File without changes
|
backend/poe/common-tasks.toml
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# This file defines common tasks that most python projects can benefit from
|
2 |
+
|
3 |
+
[tool.poe.tasks.format-isort]
|
4 |
+
help = "Format code with isort"
|
5 |
+
cmd = "isort ."
|
6 |
+
|
7 |
+
[tool.poe.tasks.format-black]
|
8 |
+
help = "Format code with black"
|
9 |
+
cmd = "black ."
|
10 |
+
|
11 |
+
[tool.poe.tasks.format]
|
12 |
+
help = "Run code formating tools"
|
13 |
+
sequence = ["format-isort", "format-black"]
|
14 |
+
|
15 |
+
[tool.poe.tasks.style-black]
|
16 |
+
help = "Validate black code style"
|
17 |
+
cmd = "black . --check --diff"
|
18 |
+
|
19 |
+
[tool.poe.tasks.style-isort]
|
20 |
+
help = "Validate isort code style"
|
21 |
+
cmd = "isort . --check --diff"
|
22 |
+
|
23 |
+
[tool.poe.tasks.style]
|
24 |
+
help = "Validate code style"
|
25 |
+
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"
|
33 |
+
cmd = "ruff check ."
|
34 |
+
|
35 |
+
[tool.poe.tasks.test]
|
36 |
+
help = "Run unit tests"
|
37 |
+
cmd = "pytest -p no:cacheprovider"
|
38 |
+
|
39 |
+
[tool.poe.tasks.clean]
|
40 |
+
help = "Remove automatically generated files"
|
41 |
+
cmd = """
|
42 |
+
rm -rf dist
|
43 |
+
.mypy_cache
|
44 |
+
.pytest_cache
|
45 |
+
.ruff_cache
|
46 |
+
./**/__pycache__/
|
47 |
+
./**/*.pyc
|
48 |
+
"""
|
49 |
+
|
50 |
+
[tool.poe.tasks.check]
|
51 |
+
help = "Run all checks on the code base"
|
52 |
+
sequence = ["style", "types", "lint", "clean"]
|
backend/poetry.lock
ADDED
The diff for this file is too large to render.
See raw diff
|
|
backend/pyproject.toml
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
[tool.poetry]
|
2 |
+
name = "smart-cat-assignment-backend"
|
3 |
+
version = "0.0.1"
|
4 |
+
description = "SmartCat Assignment"
|
5 |
+
authors = ["Lazar Radojevic <[email protected]>"]
|
6 |
+
readme = "README.md"
|
7 |
+
|
8 |
+
[tool.poetry.dependencies]
|
9 |
+
python = "^3.10"
|
10 |
+
mypy = "^1.8.0"
|
11 |
+
ruff = "^0.3.2"
|
12 |
+
datasets = "^2.20.0"
|
13 |
+
sentence-transformers = "^3.0.1"
|
14 |
+
numpy = "1.26.4"
|
15 |
+
fastapi = "^0.111.1"
|
16 |
+
uvicorn = "^0.30.3"
|
17 |
+
|
18 |
+
[tool.poetry.group.dev.dependencies]
|
19 |
+
black = "^24.1.1"
|
20 |
+
poethepoet = "^0.24.4"
|
21 |
+
isort = "^5.13.2"
|
22 |
+
|
23 |
+
[tool.isort]
|
24 |
+
profile = "black"
|
25 |
+
|
26 |
+
[tool.poe]
|
27 |
+
include = "./poe/common-tasks.toml"
|
backend/run.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
+
from pydantic import BaseModel
|
3 |
+
from typing import List
|
4 |
+
from src.search_engine import PromptSearchEngine
|
5 |
+
from src.prompt_loader import PromptLoader
|
6 |
+
|
7 |
+
# Constants
|
8 |
+
SEED = 42
|
9 |
+
DATA_SIZE = 100
|
10 |
+
|
11 |
+
# Initialize the prompt loader and search engine
|
12 |
+
prompts = PromptLoader(seed=SEED).load_data(size=DATA_SIZE)
|
13 |
+
engine = PromptSearchEngine(prompts)
|
14 |
+
|
15 |
+
# Initialize FastAPI
|
16 |
+
app = FastAPI()
|
17 |
+
|
18 |
+
|
19 |
+
# Request and Response Models
|
20 |
+
class QueryRequest(BaseModel):
|
21 |
+
query: str
|
22 |
+
n: int = 5
|
23 |
+
|
24 |
+
|
25 |
+
class SimilarPrompt(BaseModel):
|
26 |
+
score: float
|
27 |
+
prompt: str
|
28 |
+
|
29 |
+
|
30 |
+
class QueryResponse(BaseModel):
|
31 |
+
similar_prompts: List[SimilarPrompt]
|
32 |
+
|
33 |
+
|
34 |
+
# API endpoint
|
35 |
+
@app.post("/most_similar", response_model=QueryResponse)
|
36 |
+
async def get_most_similar(query_request: QueryRequest):
|
37 |
+
try:
|
38 |
+
similar_prompts = engine.most_similar(
|
39 |
+
query=query_request.query, n=query_request.n
|
40 |
+
)
|
41 |
+
response = QueryResponse(
|
42 |
+
similar_prompts=[
|
43 |
+
SimilarPrompt(score=score, prompt=prompt)
|
44 |
+
for score, prompt in similar_prompts
|
45 |
+
]
|
46 |
+
)
|
47 |
+
return response
|
48 |
+
except Exception as e:
|
49 |
+
raise HTTPException(status_code=500, detail=str(e))
|
{src β backend/src}/__init__.py
RENAMED
File without changes
|
{src β backend/src}/prompt_loader.py
RENAMED
File without changes
|
{src β backend/src}/search_engine.py
RENAMED
File without changes
|
{src β backend/src}/similarity_scorer.py
RENAMED
File without changes
|
{src β backend/src}/vectorizer.py
RENAMED
File without changes
|
docker-compose.yml
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
version: "3"
|
2 |
+
|
3 |
+
services:
|
4 |
+
frontend:
|
5 |
+
build: frontend
|
6 |
+
ports:
|
7 |
+
- 8501:8501
|
8 |
+
depends_on:
|
9 |
+
- backend
|
10 |
+
environment:
|
11 |
+
- API_URL=http://backend:8000/
|
12 |
+
|
13 |
+
backend:
|
14 |
+
build: backend
|
15 |
+
ports:
|
16 |
+
- 8000:8000
|
frontend/Dockerfile
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
# Use the official Python image from the Docker Hub
|
3 |
+
FROM python:3.10-slim
|
4 |
+
|
5 |
+
# Set the working directory in the container
|
6 |
+
WORKDIR /app
|
7 |
+
|
8 |
+
# Install Poetry
|
9 |
+
RUN pip install poetry
|
10 |
+
|
11 |
+
# Copy only the pyproject.toml and poetry.lock files to install dependencies first
|
12 |
+
COPY pyproject.toml poetry.lock ./
|
13 |
+
|
14 |
+
# Install dependencies using Poetry
|
15 |
+
RUN poetry config virtualenvs.create false && poetry install --only=main
|
16 |
+
|
17 |
+
# Copy the rest of the application code to the working directory
|
18 |
+
COPY . .
|
19 |
+
|
20 |
+
EXPOSE 8501
|
21 |
+
|
22 |
+
CMD ["streamlit", "run", "run.py"]
|
frontend/poe/common-tasks.toml
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# This file defines common tasks that most python projects can benefit from
|
2 |
+
|
3 |
+
[tool.poe.tasks.format-isort]
|
4 |
+
help = "Format code with isort"
|
5 |
+
cmd = "isort ."
|
6 |
+
|
7 |
+
[tool.poe.tasks.format-black]
|
8 |
+
help = "Format code with black"
|
9 |
+
cmd = "black ."
|
10 |
+
|
11 |
+
[tool.poe.tasks.format]
|
12 |
+
help = "Run code formating tools"
|
13 |
+
sequence = ["format-isort", "format-black"]
|
14 |
+
|
15 |
+
[tool.poe.tasks.style-black]
|
16 |
+
help = "Validate black code style"
|
17 |
+
cmd = "black . --check --diff"
|
18 |
+
|
19 |
+
[tool.poe.tasks.style-isort]
|
20 |
+
help = "Validate isort code style"
|
21 |
+
cmd = "isort . --check --diff"
|
22 |
+
|
23 |
+
[tool.poe.tasks.style]
|
24 |
+
help = "Validate code style"
|
25 |
+
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"
|
33 |
+
cmd = "ruff check ."
|
34 |
+
|
35 |
+
[tool.poe.tasks.test]
|
36 |
+
help = "Run unit tests"
|
37 |
+
cmd = "pytest -p no:cacheprovider"
|
38 |
+
|
39 |
+
[tool.poe.tasks.clean]
|
40 |
+
help = "Remove automatically generated files"
|
41 |
+
cmd = """
|
42 |
+
rm -rf dist
|
43 |
+
.mypy_cache
|
44 |
+
.pytest_cache
|
45 |
+
.ruff_cache
|
46 |
+
./**/__pycache__/
|
47 |
+
./**/*.pyc
|
48 |
+
"""
|
49 |
+
|
50 |
+
[tool.poe.tasks.check]
|
51 |
+
help = "Run all checks on the code base"
|
52 |
+
sequence = ["style", "types", "lint", "clean"]
|
frontend/poetry.lock
ADDED
The diff for this file is too large to render.
See raw diff
|
|
frontend/pyproject.toml
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
[tool.poetry]
|
2 |
+
name = "smart-cat-assignment-frontend"
|
3 |
+
version = "0.0.1"
|
4 |
+
description = "SmartCat Assignment"
|
5 |
+
authors = ["Lazar Radojevic <[email protected]>"]
|
6 |
+
readme = "README.md"
|
7 |
+
|
8 |
+
[tool.poetry.dependencies]
|
9 |
+
python = "^3.10"
|
10 |
+
mypy = "^1.8.0"
|
11 |
+
ruff = "^0.3.2"
|
12 |
+
streamlit = "^1.37.0"
|
13 |
+
|
14 |
+
[tool.poetry.group.dev.dependencies]
|
15 |
+
black = "^24.1.1"
|
16 |
+
poethepoet = "^0.24.4"
|
17 |
+
isort = "^5.13.2"
|
18 |
+
|
19 |
+
[tool.isort]
|
20 |
+
profile = "black"
|
21 |
+
|
22 |
+
[tool.poe]
|
23 |
+
include = "./poe/common-tasks.toml"
|
frontend/run.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
from typing import List, Tuple
|
4 |
+
import os
|
5 |
+
|
6 |
+
API_URL = os.getenv("API_URL")
|
7 |
+
|
8 |
+
|
9 |
+
def get_similar_prompts(query: str, n: int) -> List[Tuple[float, str]]:
|
10 |
+
try:
|
11 |
+
response = requests.post(f"{API_URL}/most_similar", json={"query": query, "n": n})
|
12 |
+
if response.status_code == 200:
|
13 |
+
return response.json().get("similar_prompts", [])
|
14 |
+
else:
|
15 |
+
st.error(f"Error: {response.status_code} - {response.text}")
|
16 |
+
return []
|
17 |
+
except requests.exceptions.RequestException as e:
|
18 |
+
st.error(f"Request error: {e}")
|
19 |
+
return []
|
20 |
+
|
21 |
+
|
22 |
+
st.title("Prompt Search Engine")
|
23 |
+
|
24 |
+
query = st.text_input("Enter a prompt:")
|
25 |
+
n = st.slider("Number of similar prompts to retrieve:", 1, 20, 5)
|
26 |
+
|
27 |
+
if st.button("Search"):
|
28 |
+
if query:
|
29 |
+
response = get_similar_prompts(query, n)
|
30 |
+
if response:
|
31 |
+
st.write(f"Top {n} similar prompts:")
|
32 |
+
for query_response in response:
|
33 |
+
st.write(
|
34 |
+
f"**Score:** {query_response['score']:.4f} | **Prompt:** {query_response['prompt']}"
|
35 |
+
)
|
36 |
+
else:
|
37 |
+
st.warning("Please enter a prompt to search.")
|
main.py
DELETED
@@ -1,27 +0,0 @@
|
|
1 |
-
from fastapi import FastAPI
|
2 |
-
from fastapi.middleware.cors import CORSMiddleware
|
3 |
-
|
4 |
-
app = FastAPI(title="Deploying FastAPI Apps on Huggingface")
|
5 |
-
|
6 |
-
|
7 |
-
app.add_middleware(
|
8 |
-
CORSMiddleware,
|
9 |
-
allow_origins=["*"],
|
10 |
-
allow_credentials=True,
|
11 |
-
allow_methods=["*"],
|
12 |
-
allow_headers=["*"],
|
13 |
-
)
|
14 |
-
|
15 |
-
|
16 |
-
@app.get("/", tags=["Home"])
|
17 |
-
def api_home():
|
18 |
-
return {"detail": "Welcome to FastAPI TextGen Tutorial!"}
|
19 |
-
|
20 |
-
|
21 |
-
@app.post(
|
22 |
-
"/api/generate",
|
23 |
-
summary="Generate text from prompt",
|
24 |
-
tags=["Generate"],
|
25 |
-
)
|
26 |
-
def inference(input_prompt: str):
|
27 |
-
return "Hello"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
DELETED
@@ -1,8 +0,0 @@
|
|
1 |
-
mypy==1.8.0
|
2 |
-
ruff==0.3.2
|
3 |
-
datasets==2.20.0
|
4 |
-
sentence-transformers==3.0.1
|
5 |
-
numpy==1.26.4
|
6 |
-
fastapi==0.111.1
|
7 |
-
uvicorn==0.30.3
|
8 |
-
streamlit==1.37.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
run.py
DELETED
@@ -1,4 +0,0 @@
|
|
1 |
-
import uvicorn
|
2 |
-
|
3 |
-
if __name__ == "__main__":
|
4 |
-
uvicorn.run("main:app", host="0.0.0.0", port=7860, reload=True)
|
|
|
|
|
|
|
|
|
|