Spaces:
Sleeping
Sleeping
upload
Browse files- Dockerfile +16 -0
- README.md +1 -10
- app.py +28 -0
- gemini.py +36 -0
- requirements.txt +4 -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 --no-cache-dir --upgrade -r /requirements.txt
|
12 |
+
|
13 |
+
# Start the FastAPI app on port 7860, the default port expected by Spaces
|
14 |
+
CMD ["gunicorn", "writing_api:app", "--host", "0.0.0.0", "--port", "7860", "--workers", "4", "--worker-class", "uvicorn.workers.UvicornWorker", "--bind", "0.0.0.0:8300", "--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 @@
|
|
1 |
-
|
2 |
-
title: Gemini Gate
|
3 |
-
emoji: 💻
|
4 |
-
colorFrom: indigo
|
5 |
-
colorTo: green
|
6 |
-
sdk: docker
|
7 |
-
pinned: false
|
8 |
-
---
|
9 |
-
|
10 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
+
# test_project_ielts
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pydantic import BaseModel
|
2 |
+
from fastapi import HTTPException
|
3 |
+
import os
|
4 |
+
import traceback
|
5 |
+
from fastapi.middleware.cors import CORSMiddleware
|
6 |
+
from gemini import complete_gemini_async
|
7 |
+
|
8 |
+
class GenItem(BaseModel):
|
9 |
+
chat:list
|
10 |
+
key:str
|
11 |
+
params:dict
|
12 |
+
|
13 |
+
app = FastAPI()
|
14 |
+
origins = ["*"]
|
15 |
+
app.add_middleware(
|
16 |
+
CORSMiddleware,
|
17 |
+
allow_origins=origins,
|
18 |
+
allow_credentials=True,
|
19 |
+
allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"],
|
20 |
+
allow_headers=["*"],
|
21 |
+
)
|
22 |
+
@app.post("/complete")
|
23 |
+
async def complete(item: GenItem):
|
24 |
+
try:
|
25 |
+
result = await complete_gemini_async(item.chat, item.key, item.params)
|
26 |
+
except Exception as e:
|
27 |
+
raise HTTPException(status_code=500, detail="An error occurred: {}".format(traceback.format_exc()))
|
28 |
+
|
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}
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
langchain==0.1.13
|
2 |
+
fastapi==0.110.0
|
3 |
+
gunicorn==21.2.0
|
4 |
+
passlib==1.7.4
|