Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,52 @@
|
|
1 |
-
from fastapi import FastAPI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
app = FastAPI()
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
@app.get("/")
|
6 |
-
def
|
7 |
-
return {"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# from fastapi import FastAPI
|
2 |
+
|
3 |
+
# app = FastAPI()
|
4 |
+
|
5 |
+
# @app.get("/")
|
6 |
+
# def greet_json():
|
7 |
+
# return {"Hello": "World!"}
|
8 |
+
|
9 |
+
|
10 |
+
from fastapi import FastAPI, HTTPException
|
11 |
+
from pydantic import BaseModel
|
12 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
13 |
+
import sqlite3
|
14 |
+
import torch
|
15 |
|
16 |
app = FastAPI()
|
17 |
|
18 |
+
# Load the DeepSeek model and tokenizer
|
19 |
+
MODEL_NAME = "deepseek-ai/deepseek-coder-1.3b-instruct"
|
20 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
21 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, torch_dtype=torch.float16).to("cpu") # Use "cuda" if available
|
22 |
+
|
23 |
+
|
24 |
+
def generate_sql_query(user_input: str) -> str:
|
25 |
+
"""
|
26 |
+
Generate an SQL query from a natural language query using the DeepSeek model.
|
27 |
+
"""
|
28 |
+
inputs = tokenizer(user_input, return_tensors="pt")
|
29 |
+
outputs = model.generate(**inputs, max_length=100)
|
30 |
+
sql_query = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
31 |
+
return sql_query
|
32 |
+
|
33 |
+
|
34 |
+
@app.post("/chat")
|
35 |
+
def chat(request: ChatRequest):
|
36 |
+
user_input = request.message
|
37 |
+
|
38 |
+
sql_query = generate_sql_query(user_input)
|
39 |
+
print(f"Generated SQL Query: {sql_query}")
|
40 |
+
|
41 |
+
return {"response": sql_query}
|
42 |
+
|
43 |
@app.get("/")
|
44 |
+
def home():
|
45 |
+
return {"message": "DeepSeek SQL Query API is running"}
|
46 |
+
|
47 |
+
|
48 |
+
# Run the API
|
49 |
+
# if __name__ == "__main__":
|
50 |
+
# import uvicorn
|
51 |
+
# uvicorn.run(app, host="0.0.0.0", port=8000)
|
52 |
+
|