xidu commited on
Commit
46b67ed
·
1 Parent(s): 4a5a35d

deploy: Final minimal single API service

Browse files
Files changed (4) hide show
  1. Dockerfile +14 -0
  2. README.md +6 -4
  3. app.py +34 -0
  4. requirements.txt +3 -0
Dockerfile ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9-slim
2
+
3
+ WORKDIR /app
4
+
5
+ COPY ./requirements.txt /app/requirements.txt
6
+ RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt
7
+
8
+ COPY ./app.py /app/
9
+
10
+ # Hugging Face 要求对外暴露 7860 端口
11
+ EXPOSE 7860
12
+
13
+ # 直接运行 uvicorn,监听对外的主端口
14
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
README.md CHANGED
@@ -1,10 +1,12 @@
1
  ---
2
- title: Api1
3
- emoji: 📉
4
- colorFrom: pink
5
  colorTo: blue
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: Single GenAI API Service
3
+ emoji: 🎯
4
+ colorFrom: green
5
  colorTo: blue
6
  sdk: docker
7
  pinned: false
8
  ---
9
+ This is a minimal FastAPI application that connects to the Google GenAI API.
10
 
11
+ - **GET /**: Check service status.
12
+ - **POST /chat**: Send a prompt to the AI.
app.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from pydantic import BaseModel
3
+ import google.generativeai as genai
4
+ import os
5
+
6
+ # !!! 重要 !!!
7
+ # 强烈建议通过 Space Secrets (https://huggingface.co/docs/hub/spaces-secrets) 来管理 API Key
8
+ # 1. 在 Space 的 Settings -> Secrets 中创建一个名为 GOOGLE_API_KEY 的 Secret
9
+ # 2. 然后使用下面这行代码来读取它:
10
+ # genai.configure(api_key=os.environ.get("GOOGLE_API_KEY"))
11
+ #
12
+ # 为了方便首次测试,您可以先临时硬编码:
13
+ genai.configure(api_key="YOUR_GOOGLE_API_KEY_HERE")
14
+
15
+
16
+ app = FastAPI(title="Single GenAI API")
17
+
18
+ class ChatRequest(BaseModel):
19
+ prompt: str
20
+
21
+ @app.get("/")
22
+ def read_root():
23
+ return {"status": "ok", "message": "The single API service is running successfully!"}
24
+
25
+ @app.post("/chat")
26
+ async def chat_handler(request: ChatRequest):
27
+ try:
28
+ model = genai.GenerativeModel('gemini-pro')
29
+ response = model.generate_content(request.prompt)
30
+ return {"response": response.text}
31
+ except Exception as e:
32
+ # 打印详细错误到日志,方便调试
33
+ print(f"An error occurred: {e}")
34
+ raise HTTPException(status_code=500, detail=str(e))
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ google-generativeai