Spaces:
Sleeping
Sleeping
phamquang-hieu
commited on
Commit
•
50c34da
1
Parent(s):
f721a44
init
Browse files- Dockerfile +16 -0
- README.md +6 -7
- app.py +30 -0
- gemini.py +36 -0
- gitattributes +35 -0
- requirements.txt +8 -0
Dockerfile
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Use the official Python 3.10.9 image
|
2 |
+
FROM python:3.10.9
|
3 |
+
|
4 |
+
# Copy the current directory contents into the container at .
|
5 |
+
COPY . .
|
6 |
+
|
7 |
+
# Set the working directory to /
|
8 |
+
WORKDIR /
|
9 |
+
|
10 |
+
# Install requirements.txt
|
11 |
+
RUN pip install -r requirements.txt
|
12 |
+
|
13 |
+
# Start the FastAPI app on port 7860, the default port expected by Spaces
|
14 |
+
CMD ["gunicorn", "app:app", "--workers", "4", "--worker-class", "uvicorn.workers.UvicornWorker", "--bind", "0.0.0.0:7860", "--timeout", "60"]
|
15 |
+
|
16 |
+
#gunicorn writing_api:app --workers 4 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:8300 --reload --preload --timeout 60
|
README.md
CHANGED
@@ -1,10 +1,9 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: docker
|
|
|
7 |
pinned: false
|
8 |
-
---
|
9 |
-
|
10 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: IELTS TRAINER
|
3 |
+
emoji: 📝
|
4 |
+
colorFrom: blue
|
5 |
+
colorTo: green
|
6 |
sdk: docker
|
7 |
+
app_file: app.py
|
8 |
pinned: false
|
9 |
+
---
|
|
|
|
app.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
from pydantic import BaseModel
|
3 |
+
from fastapi import HTTPException
|
4 |
+
import os
|
5 |
+
import traceback
|
6 |
+
from fastapi.middleware.cors import CORSMiddleware
|
7 |
+
from gemini import complete_gemini_async
|
8 |
+
|
9 |
+
class GenItem(BaseModel):
|
10 |
+
chat:list
|
11 |
+
key:str
|
12 |
+
params:dict
|
13 |
+
|
14 |
+
app = FastAPI()
|
15 |
+
origins = ["*"]
|
16 |
+
app.add_middleware(
|
17 |
+
CORSMiddleware,
|
18 |
+
allow_origins=origins,
|
19 |
+
allow_credentials=True,
|
20 |
+
allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"],
|
21 |
+
allow_headers=["*"],
|
22 |
+
)
|
23 |
+
@app.post("/complete")
|
24 |
+
async def complete(item: GenItem):
|
25 |
+
try:
|
26 |
+
result = await complete_gemini_async(item.chat, item.key, item.params)
|
27 |
+
return result
|
28 |
+
except Exception as e:
|
29 |
+
raise HTTPException(status_code=500, detail="An error occurred: {}".format(traceback.format_exc()))
|
30 |
+
|
gemini.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import os
|
3 |
+
import httpx
|
4 |
+
import asyncio
|
5 |
+
|
6 |
+
GEMINI_URL = "https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent"
|
7 |
+
|
8 |
+
async def complete_gemini_async(chat, key, params={}):
|
9 |
+
data = {
|
10 |
+
"contents": chat,
|
11 |
+
"generationConfig": {
|
12 |
+
"stopSequences": ["Title"],
|
13 |
+
"temperature": 1.0 if "temp" not in params else params["temp"],
|
14 |
+
"maxOutputTokens": 2048 if "max_length" not in params else params["max_length"],
|
15 |
+
"topP": 0.8 if "top_p" not in params else params["top_p"],
|
16 |
+
"topK": 10 if "top_k" not in params else params["top_k"]
|
17 |
+
}
|
18 |
+
}
|
19 |
+
params = {'key': key}
|
20 |
+
headers = {"Content-Type": "application/json"}
|
21 |
+
result = None
|
22 |
+
try:
|
23 |
+
async with httpx.AsyncClient() as client:
|
24 |
+
result = await client.post(GEMINI_URL, params=params, json=data, headers=headers, timeout=60)
|
25 |
+
result.raise_for_status()
|
26 |
+
result = result.json()["candidates"][0]["content"]["parts"][0]["text"]
|
27 |
+
return {"response":result}
|
28 |
+
except requests.RequestException as e:
|
29 |
+
if e.status_code == 429:
|
30 |
+
await asyncio.sleep(60)
|
31 |
+
#rint(f"Error making Gemini API request: {e}")
|
32 |
+
async with httpx.AsyncClient() as client:
|
33 |
+
result = await client.post(GEMINI_URL, params=params, json=data, headers=headers, timeout=60)
|
34 |
+
result.raise_for_status()
|
35 |
+
result = result.json()["candidates"][0]["content"]["parts"][0]["text"]
|
36 |
+
return {"response":result}
|
gitattributes
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi
|
2 |
+
gunicorn
|
3 |
+
passlib
|
4 |
+
langchain
|
5 |
+
uvicorn
|
6 |
+
thefuzz
|
7 |
+
python-dotenv
|
8 |
+
httpx
|