Spaces:
Sleeping
Sleeping
PhilSad
commited on
Commit
·
a3c44d4
1
Parent(s):
da4dff9
convert to flask + cors
Browse files- app.py +28 -25
- requirements.txt +2 -1
app.py
CHANGED
|
@@ -1,45 +1,48 @@
|
|
| 1 |
-
from
|
| 2 |
-
from fastapi import FastAPI, HTTPException
|
| 3 |
-
from pydantic import BaseModel
|
| 4 |
import os
|
| 5 |
import httpx
|
|
|
|
| 6 |
|
| 7 |
# Load Mistral API token from environment variables
|
| 8 |
MISTRAL_API_TOKEN = os.getenv("MISTRAL_API_TOKEN")
|
| 9 |
if not MISTRAL_API_TOKEN:
|
| 10 |
raise EnvironmentError("MISTRAL_API_TOKEN environment variable not set.")
|
| 11 |
|
| 12 |
-
# Initialize
|
| 13 |
-
app =
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
# system_prompt : str
|
| 18 |
-
# input_text: str
|
| 19 |
-
|
| 20 |
-
@app.post("/proxy/mistral")
|
| 21 |
-
async def proxy_mistral(request_data: Dict):
|
| 22 |
"""Proxy endpoint to send requests to Mistral API."""
|
|
|
|
|
|
|
| 23 |
url = "https://api.mistral.ai/v1/chat/completions"
|
| 24 |
headers = {
|
| 25 |
"Authorization": f"Bearer {MISTRAL_API_TOKEN}",
|
| 26 |
"Content-Type": "application/json"
|
| 27 |
}
|
| 28 |
-
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
|
|
|
|
| 32 |
print(request_data)
|
| 33 |
|
| 34 |
-
# Send the request to the Mistral API
|
| 35 |
-
|
| 36 |
-
response =
|
| 37 |
-
print("my response" , response)
|
| 38 |
|
| 39 |
-
#
|
| 40 |
if response.status_code != 200:
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, abort
|
|
|
|
|
|
|
| 2 |
import os
|
| 3 |
import httpx
|
| 4 |
+
from flask_cors import CORS
|
| 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 Flask app
|
| 12 |
+
app = Flask(__name__)
|
| 13 |
+
CORS(app)
|
| 14 |
|
| 15 |
+
@app.route("/proxy/mistral", methods=["POST"])
|
| 16 |
+
def proxy_mistral():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
"""Proxy endpoint to send requests to Mistral API."""
|
| 18 |
+
|
| 19 |
+
# Mistral endpoint and headers
|
| 20 |
url = "https://api.mistral.ai/v1/chat/completions"
|
| 21 |
headers = {
|
| 22 |
"Authorization": f"Bearer {MISTRAL_API_TOKEN}",
|
| 23 |
"Content-Type": "application/json"
|
| 24 |
}
|
|
|
|
| 25 |
|
| 26 |
+
# Parse the JSON body from the incoming request
|
| 27 |
+
request_data = request.get_json()
|
| 28 |
+
print("Attempting to send the request to Mistral")
|
| 29 |
print(request_data)
|
| 30 |
|
| 31 |
+
# Send the request to the Mistral API using httpx (synchronous)
|
| 32 |
+
with httpx.Client() as client:
|
| 33 |
+
response = client.post(url, headers=headers, json=request_data, timeout=None)
|
|
|
|
| 34 |
|
| 35 |
+
# If Mistral doesn’t return a 200, propagate the error
|
| 36 |
if response.status_code != 200:
|
| 37 |
+
# In Flask, you can either return an error explicitly:
|
| 38 |
+
# return (response.text, response.status_code)
|
| 39 |
+
# or raise an HTTPException via `abort()`.
|
| 40 |
+
return (response.text, response.status_code)
|
| 41 |
+
|
| 42 |
+
# Print the Mistral API response (for debug) and return it
|
| 43 |
+
print("Mistral response:", response.json())
|
| 44 |
+
return jsonify(response.json())
|
| 45 |
+
|
| 46 |
+
if __name__ == "__main__":
|
| 47 |
+
# Run the Flask app (use your desired host and port)
|
| 48 |
+
app.run(host="0.0.0.0", port=8000, debug=True)
|
requirements.txt
CHANGED
|
@@ -1,3 +1,4 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
uvicorn[standard]
|
| 3 |
httpx
|
|
|
|
| 1 |
+
flask
|
| 2 |
+
flask-cors
|
| 3 |
uvicorn[standard]
|
| 4 |
httpx
|