Speedofmastery commited on
Commit
ef20ca0
·
verified ·
1 Parent(s): 2b2343c

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. Dockerfile +7 -0
  2. README.md +18 -10
  3. app.py +163 -0
  4. requirements.txt +4 -0
Dockerfile ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ FROM python:3.10-slim
2
+ WORKDIR /app
3
+ COPY requirements.txt .
4
+ RUN pip install --no-cache-dir -r requirements.txt
5
+ COPY app.py .
6
+ EXPOSE 7860
7
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
README.md CHANGED
@@ -1,10 +1,18 @@
1
- ---
2
- title: Orynxml Simple Api
3
- emoji: 🦀
4
- colorFrom: yellow
5
- colorTo: red
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: ORYNXML Simple API
3
+ emoji: 🚀
4
+ colorFrom: blue
5
+ colorTo: purple
6
+ sdk: docker
7
+ pinned: false
8
+ ---
9
+
10
+ # ORYNXML Simple API
11
+
12
+ Working REST API for ORYNXML platform.
13
+
14
+ ## Endpoints
15
+ - POST /auth/signup
16
+ - POST /auth/login
17
+ - POST /ai/chat
18
+ - GET /health
app.py ADDED
@@ -0,0 +1,163 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ SIMPLE REST API that WORKS - No bullshit
3
+ """
4
+ from fastapi import FastAPI, HTTPException
5
+ from fastapi.middleware.cors import CORSMiddleware
6
+ from pydantic import BaseModel
7
+ from typing import Optional, List
8
+ import os
9
+ import sqlite3
10
+ import hashlib
11
+ from datetime import datetime
12
+ import requests
13
+
14
+ app = FastAPI(title="ORYNXML API", version="1.0.0")
15
+
16
+ # CORS
17
+ app.add_middleware(
18
+ CORSMiddleware,
19
+ allow_origins=["*"],
20
+ allow_credentials=True,
21
+ allow_methods=["*"],
22
+ allow_headers=["*"],
23
+ )
24
+
25
+ # HuggingFace token
26
+ HF_TOKEN = os.getenv("HF_TOKEN", "")
27
+ HF_API_URL = "https://api-inference.huggingface.co/models"
28
+
29
+ # Database
30
+ def init_db():
31
+ conn = sqlite3.connect("users.db")
32
+ c = conn.cursor()
33
+ c.execute("""CREATE TABLE IF NOT EXISTS users (
34
+ id INTEGER PRIMARY KEY,
35
+ mobile TEXT UNIQUE,
36
+ name TEXT,
37
+ password TEXT,
38
+ created_at TEXT
39
+ )""")
40
+ conn.commit()
41
+ conn.close()
42
+
43
+ init_db()
44
+
45
+ # Models
46
+ class SignupRequest(BaseModel):
47
+ mobile: str
48
+ name: str
49
+ password: str
50
+
51
+ class LoginRequest(BaseModel):
52
+ mobile: str
53
+ password: str
54
+
55
+ class ChatRequest(BaseModel):
56
+ message: str
57
+ model: Optional[str] = "Qwen/Qwen2.5-72B-Instruct"
58
+
59
+ def hash_pass(p):
60
+ return hashlib.sha256(p.encode()).hexdigest()
61
+
62
+ # Routes
63
+ @app.get("/")
64
+ def root():
65
+ return {
66
+ "status": "running",
67
+ "message": "ORYNXML API",
68
+ "endpoints": ["/health", "/auth/signup", "/auth/login", "/ai/chat"]
69
+ }
70
+
71
+ @app.get("/health")
72
+ def health():
73
+ return {
74
+ "status": "healthy",
75
+ "timestamp": datetime.now().isoformat(),
76
+ "hf_token": "configured" if HF_TOKEN else "missing"
77
+ }
78
+
79
+ @app.post("/auth/signup")
80
+ def signup(req: SignupRequest):
81
+ try:
82
+ conn = sqlite3.connect("users.db")
83
+ c = conn.cursor()
84
+ c.execute("SELECT mobile FROM users WHERE mobile=?", (req.mobile,))
85
+ if c.fetchone():
86
+ conn.close()
87
+ raise HTTPException(400, "Mobile already registered")
88
+
89
+ c.execute("INSERT INTO users (mobile, name, password, created_at) VALUES (?,?,?,?)",
90
+ (req.mobile, req.name, hash_pass(req.password), datetime.now().isoformat()))
91
+ conn.commit()
92
+ conn.close()
93
+ return {"success": True, "message": "Account created"}
94
+ except HTTPException:
95
+ raise
96
+ except Exception as e:
97
+ raise HTTPException(500, str(e))
98
+
99
+ @app.post("/auth/login")
100
+ def login(req: LoginRequest):
101
+ try:
102
+ conn = sqlite3.connect("users.db")
103
+ c = conn.cursor()
104
+ c.execute("SELECT name, password FROM users WHERE mobile=?", (req.mobile,))
105
+ result = c.fetchone()
106
+ conn.close()
107
+
108
+ if not result or result[1] != hash_pass(req.password):
109
+ raise HTTPException(401, "Invalid credentials")
110
+
111
+ return {
112
+ "success": True,
113
+ "message": "Login successful",
114
+ "user": {"mobile": req.mobile, "name": result[0]},
115
+ "token": hash_pass(req.mobile)[:32]
116
+ }
117
+ except HTTPException:
118
+ raise
119
+ except Exception as e:
120
+ raise HTTPException(500, str(e))
121
+
122
+ @app.post("/ai/chat")
123
+ def ai_chat(req: ChatRequest):
124
+ """Call HuggingFace Inference API directly"""
125
+ try:
126
+ # Use requests library to call HF API directly
127
+ url = f"{HF_API_URL}/{req.model}"
128
+ headers = {"Authorization": f"Bearer {HF_TOKEN}"}
129
+
130
+ payload = {
131
+ "inputs": req.message,
132
+ "parameters": {
133
+ "max_new_tokens": 500,
134
+ "temperature": 0.7,
135
+ "return_full_text": False
136
+ }
137
+ }
138
+
139
+ response = requests.post(url, headers=headers, json=payload, timeout=30)
140
+
141
+ if response.status_code == 200:
142
+ result = response.json()
143
+ # Extract text from response
144
+ if isinstance(result, list) and len(result) > 0:
145
+ text = result[0].get("generated_text", str(result))
146
+ else:
147
+ text = str(result)
148
+
149
+ return {
150
+ "success": True,
151
+ "response": text,
152
+ "model": req.model,
153
+ "timestamp": datetime.now().isoformat()
154
+ }
155
+ else:
156
+ raise HTTPException(500, f"HF API error: {response.status_code} - {response.text}")
157
+
158
+ except Exception as e:
159
+ raise HTTPException(500, f"AI chat failed: {str(e)}")
160
+
161
+ if __name__ == "__main__":
162
+ import uvicorn
163
+ uvicorn.run(app, host="0.0.0.0", port=7860)
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ fastapi==0.115.0
2
+ uvicorn[standard]==0.30.6
3
+ pydantic==2.9.2
4
+ requests==2.32.3