Spaces:
Sleeping
Sleeping
Commit ·
e2412ee
1
Parent(s): 02b1777
commit 0000001
Browse files- app.py +99 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, Response
|
| 2 |
+
from flasgger import Swagger, swag_from
|
| 3 |
+
import os
|
| 4 |
+
import requests
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
|
| 7 |
+
load_dotenv()
|
| 8 |
+
|
| 9 |
+
app = Flask(__name__)
|
| 10 |
+
swagger = Swagger(app)
|
| 11 |
+
|
| 12 |
+
HF_TOKEN = os.environ.get("CHAT_MATE")
|
| 13 |
+
|
| 14 |
+
@app.route("/chat-stream", methods=["POST"])
|
| 15 |
+
@swag_from({
|
| 16 |
+
'tags': ['Chat'],
|
| 17 |
+
'consumes': ['application/json'],
|
| 18 |
+
'summary': 'Stream assistant reply',
|
| 19 |
+
'description': 'Send a message and optional history, receive streamed text response.',
|
| 20 |
+
'parameters': [{
|
| 21 |
+
'name': 'body',
|
| 22 |
+
'in': 'body',
|
| 23 |
+
'required': True,
|
| 24 |
+
'schema': {
|
| 25 |
+
'type': 'object',
|
| 26 |
+
'properties': {
|
| 27 |
+
'message': {'type': 'string'},
|
| 28 |
+
'history': {
|
| 29 |
+
'type': 'array',
|
| 30 |
+
'items': {
|
| 31 |
+
'type': 'object',
|
| 32 |
+
'properties': {
|
| 33 |
+
'role': {'type': 'string'},
|
| 34 |
+
'content': {'type': 'string'}
|
| 35 |
+
}
|
| 36 |
+
}
|
| 37 |
+
}
|
| 38 |
+
},
|
| 39 |
+
'required': ['message']
|
| 40 |
+
}
|
| 41 |
+
}],
|
| 42 |
+
'responses': {
|
| 43 |
+
200: {
|
| 44 |
+
'description': 'Streamed reply',
|
| 45 |
+
'content': {'text/plain': {}}
|
| 46 |
+
}
|
| 47 |
+
}
|
| 48 |
+
})
|
| 49 |
+
def chat_stream():
|
| 50 |
+
data = request.get_json()
|
| 51 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 52 |
+
response = requests.post(
|
| 53 |
+
"https://fredericksundeep-aichatmatedev.hf.space/chat-stream",
|
| 54 |
+
json=data,
|
| 55 |
+
headers=headers,
|
| 56 |
+
stream=True
|
| 57 |
+
)
|
| 58 |
+
return Response(response.iter_lines(decode_unicode=True), mimetype="text/plain")
|
| 59 |
+
|
| 60 |
+
@app.route("/chat-stream-doc", methods=["POST"])
|
| 61 |
+
@swag_from({
|
| 62 |
+
'tags': ['Chat'],
|
| 63 |
+
'consumes': ['multipart/form-data'],
|
| 64 |
+
'summary': 'Upload requirement doc & generate code ZIP',
|
| 65 |
+
'description': 'Upload PDF/TXT with tech preferences. Returns ZIP file.',
|
| 66 |
+
'parameters': [
|
| 67 |
+
{'name': 'file', 'in': 'formData', 'type': 'file', 'required': True},
|
| 68 |
+
{'name': 'frontend', 'in': 'formData', 'type': 'string', 'required': True},
|
| 69 |
+
{'name': 'backend', 'in': 'formData', 'type': 'string', 'required': True},
|
| 70 |
+
{'name': 'database', 'in': 'formData', 'type': 'string', 'required': True}
|
| 71 |
+
],
|
| 72 |
+
'responses': {
|
| 73 |
+
200: {
|
| 74 |
+
'description': 'ZIP file',
|
| 75 |
+
'content': {'application/zip': {}}
|
| 76 |
+
}
|
| 77 |
+
}
|
| 78 |
+
})
|
| 79 |
+
def chat_stream_doc():
|
| 80 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 81 |
+
form_data = {
|
| 82 |
+
"frontend": request.form.get("frontend"),
|
| 83 |
+
"backend": request.form.get("backend"),
|
| 84 |
+
"database": request.form.get("database")
|
| 85 |
+
}
|
| 86 |
+
files = {'file': request.files['file']}
|
| 87 |
+
response = requests.post(
|
| 88 |
+
"https://fredericksundeep-aimateapis.hf.space/chat-stream-doc",
|
| 89 |
+
headers=headers,
|
| 90 |
+
data=form_data,
|
| 91 |
+
files=files
|
| 92 |
+
)
|
| 93 |
+
return (response.content, response.status_code, {
|
| 94 |
+
"Content-Type": response.headers.get("Content-Type", "application/octet-stream"),
|
| 95 |
+
"Content-Disposition": response.headers.get("Content-Disposition", "attachment; filename=project.zip")
|
| 96 |
+
})
|
| 97 |
+
|
| 98 |
+
if __name__ == "__main__":
|
| 99 |
+
app.run(debug=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
flask
|
| 2 |
+
flasgger
|
| 3 |
+
requests
|
| 4 |
+
python-dotenv
|