Spaces:
				
			
			
	
			
			
		Sleeping
		
	
	
	
			
			
	
	
	
	
		
		
		Sleeping
		
	add project files
Browse files- Dockerfile +16 -0
- README.md +3 -3
- app.py +27 -0
- requirements.txt +3 -0
- test.py +18 -0
    	
        Dockerfile
    ADDED
    
    | @@ -0,0 +1,16 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            # Read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
         | 
| 2 | 
            +
            # you will also find guides on how best to write your Dockerfile
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            FROM python:3.9
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            RUN useradd -m -u 1000 user
         | 
| 7 | 
            +
            USER user
         | 
| 8 | 
            +
            ENV PATH="/home/user/.local/bin:$PATH"
         | 
| 9 | 
            +
             | 
| 10 | 
            +
            WORKDIR /app
         | 
| 11 | 
            +
             | 
| 12 | 
            +
            COPY --chown=user ./requirements.txt requirements.txt
         | 
| 13 | 
            +
            RUN pip install --no-cache-dir --upgrade -r requirements.txt
         | 
| 14 | 
            +
             | 
| 15 | 
            +
            COPY --chown=user . /app
         | 
| 16 | 
            +
            CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
         | 
    	
        README.md
    CHANGED
    
    | @@ -1,8 +1,8 @@ | |
| 1 | 
             
            ---
         | 
| 2 | 
             
            title: Sbert Embedding
         | 
| 3 | 
            -
            emoji:  | 
| 4 | 
            -
            colorFrom:  | 
| 5 | 
            -
            colorTo:  | 
| 6 | 
             
            sdk: docker
         | 
| 7 | 
             
            pinned: false
         | 
| 8 | 
             
            ---
         | 
|  | |
| 1 | 
             
            ---
         | 
| 2 | 
             
            title: Sbert Embedding
         | 
| 3 | 
            +
            emoji: 🦀
         | 
| 4 | 
            +
            colorFrom: green
         | 
| 5 | 
            +
            colorTo: pink
         | 
| 6 | 
             
            sdk: docker
         | 
| 7 | 
             
            pinned: false
         | 
| 8 | 
             
            ---
         | 
    	
        app.py
    ADDED
    
    | @@ -0,0 +1,27 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            from fastapi import FastAPI
         | 
| 2 | 
            +
            from pydantic import BaseModel
         | 
| 3 | 
            +
            from sentence_transformers import SentenceTransformer
         | 
| 4 | 
            +
            from typing import List
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            # Initialize the model
         | 
| 7 | 
            +
            model = SentenceTransformer("PartAI/Tooka-SBERT")
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            # Create the FastAPI app
         | 
| 10 | 
            +
            app = FastAPI()
         | 
| 11 | 
            +
             | 
| 12 | 
            +
            # Pydantic model for input data
         | 
| 13 | 
            +
            class TextInput(BaseModel):
         | 
| 14 | 
            +
                sentences: List[str]
         | 
| 15 | 
            +
             | 
| 16 | 
            +
            @app.get('/')
         | 
| 17 | 
            +
            def index():
         | 
| 18 | 
            +
                return {'message': 'Sentence embedding API.'}
         | 
| 19 | 
            +
             | 
| 20 | 
            +
            # Endpoint to get embeddings
         | 
| 21 | 
            +
            @app.post("/get_embeddings")
         | 
| 22 | 
            +
            async def get_embeddings(input_data: TextInput):
         | 
| 23 | 
            +
                # Get embeddings for the input sentences
         | 
| 24 | 
            +
                embeddings = model.encode(input_data.sentences)
         | 
| 25 | 
            +
                return {"embeddings": embeddings.tolist()}
         | 
| 26 | 
            +
             | 
| 27 | 
            +
            # To run the app, save this code to a file, and then run `uvicorn filename:app --reload`
         | 
    	
        requirements.txt
    ADDED
    
    | @@ -0,0 +1,3 @@ | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            fastapi
         | 
| 2 | 
            +
            uvicorn[standard]
         | 
| 3 | 
            +
            sentence_transformers
         | 
    	
        test.py
    ADDED
    
    | @@ -0,0 +1,18 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            import requests
         | 
| 2 | 
            +
            import json
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            url = "https://diginext-sbert-embedding.hf.space/get_embeddings"
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            payload = json.dumps({
         | 
| 7 | 
            +
              "sentences": [
         | 
| 8 | 
            +
                "همه چی خوبه؟"
         | 
| 9 | 
            +
              ]
         | 
| 10 | 
            +
            })
         | 
| 11 | 
            +
            headers = {
         | 
| 12 | 
            +
              'accept': 'application/json',
         | 
| 13 | 
            +
              'Content-Type': 'application/json'
         | 
| 14 | 
            +
            }
         | 
| 15 | 
            +
             | 
| 16 | 
            +
            response = requests.request("POST", url, headers=headers, data=payload)
         | 
| 17 | 
            +
             | 
| 18 | 
            +
            print(response.text)
         | 
