Spaces:
Running
Running
PhilSad
commited on
Commit
Β·
9ada063
1
Parent(s):
8c346dd
save
Browse files- DadKnowsSiegeBest-api +1 -0
- Dockerfile +14 -0
- README.md +5 -4
- app.py +55 -0
- requirements.txt +2 -0
DadKnowsSiegeBest-api
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
Subproject commit f87ff9e0af3e0fbb9a562fce7421d933a164a1af
|
Dockerfile
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
FROM python:3.9
|
3 |
+
|
4 |
+
RUN useradd -m -u 1000 user
|
5 |
+
USER user
|
6 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
7 |
+
|
8 |
+
WORKDIR /app
|
9 |
+
|
10 |
+
COPY --chown=user ./requirements.txt requirements.txt
|
11 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
12 |
+
|
13 |
+
COPY --chown=user . /app
|
14 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
README.md
CHANGED
@@ -1,10 +1,11 @@
|
|
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: DadKnowsSiegeBest Api
|
3 |
+
emoji: π
|
4 |
+
colorFrom: gray
|
5 |
+
colorTo: yellow
|
6 |
sdk: docker
|
7 |
pinned: false
|
8 |
+
short_description: The backend for our submission for the Mistral GameJam
|
9 |
---
|
10 |
|
11 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
+
from pydantic import BaseModel
|
3 |
+
import os
|
4 |
+
import httpx
|
5 |
+
|
6 |
+
# Load Mistral API token from environment variables
|
7 |
+
MISTRAL_API_TOKEN = os.getenv("MISTRAL_API_TOKEN")
|
8 |
+
if not MISTRAL_API_TOKEN:
|
9 |
+
raise EnvironmentError("MISTRAL_API_TOKEN environment variable not set.")
|
10 |
+
|
11 |
+
# Initialize FastAPI app
|
12 |
+
app = FastAPI()
|
13 |
+
|
14 |
+
# Define the request model for input validation
|
15 |
+
class MistralRequest(BaseModel):
|
16 |
+
system_prompt : str
|
17 |
+
input_text: str
|
18 |
+
|
19 |
+
@app.post("/proxy/mistral")
|
20 |
+
async def proxy_mistral(request_data: MistralRequest):
|
21 |
+
"""Proxy endpoint to send requests to Mistral API."""
|
22 |
+
url = "https://api.mistral.ai/v1/chat/completions"
|
23 |
+
headers = {
|
24 |
+
"Authorization": f"Bearer {MISTRAL_API_TOKEN}",
|
25 |
+
"Content-Type": "application/json"
|
26 |
+
}
|
27 |
+
|
28 |
+
# Construct the data payload
|
29 |
+
payload = {
|
30 |
+
"messages": [
|
31 |
+
{
|
32 |
+
"role" : "system",
|
33 |
+
"content" : request_data.system_prompt
|
34 |
+
},
|
35 |
+
{
|
36 |
+
"role": "user",
|
37 |
+
"content": request_data.input_text
|
38 |
+
}
|
39 |
+
]
|
40 |
+
}
|
41 |
+
|
42 |
+
try:
|
43 |
+
# Send the request to the Mistral API
|
44 |
+
async with httpx.AsyncClient() as client:
|
45 |
+
response = await client.post(url, headers=headers, json=payload)
|
46 |
+
|
47 |
+
# Check the response status
|
48 |
+
if response.status_code != 200:
|
49 |
+
raise HTTPException(status_code=response.status_code, detail=response.text)
|
50 |
+
|
51 |
+
# Return the Mistral API response
|
52 |
+
return response.json()
|
53 |
+
|
54 |
+
except Exception as e:
|
55 |
+
raise HTTPException(status_code=500, detail=str(e))
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
fastapi
|
2 |
+
uvicorn[standard]
|