Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -44,23 +44,14 @@
|
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
import os
|
| 55 |
-
import asyncio
|
| 56 |
from fastapi import FastAPI
|
| 57 |
from pydantic import BaseModel
|
| 58 |
-
import gradio as gr
|
| 59 |
from llama_cpp import Llama
|
| 60 |
from huggingface_hub import snapshot_download
|
| 61 |
|
| 62 |
-
# Optionally increase the HTTP timeout via an environment variable
|
| 63 |
-
os.environ["HF_HUB_HTTP_TIMEOUT"] = "3600"
|
| 64 |
|
| 65 |
# Download the model repository locally; this call blocks until the download completes.
|
| 66 |
local_repo = snapshot_download(
|
|
@@ -74,19 +65,20 @@ local_repo = snapshot_download(
|
|
| 74 |
llm = Llama.from_pretrained(
|
| 75 |
repo_id="DebabrataHalder/mysqlmodel", # valid repo id format
|
| 76 |
filename="mysqlmodel.gguf",
|
| 77 |
-
cache_dir=local_repo,
|
| 78 |
-
n_ctx=2048,
|
| 79 |
-
local_files_only=True
|
| 80 |
)
|
| 81 |
|
|
|
|
| 82 |
def chat(prompt: str) -> str:
|
| 83 |
output = llm(prompt)
|
| 84 |
return output["choices"][0]["text"]
|
| 85 |
|
| 86 |
-
#
|
| 87 |
app = FastAPI(title="MySQL LLM Chat API")
|
| 88 |
|
| 89 |
-
# Define
|
| 90 |
class ChatRequest(BaseModel):
|
| 91 |
prompt: str
|
| 92 |
|
|
@@ -94,27 +86,13 @@ class ChatResponse(BaseModel):
|
|
| 94 |
response: str
|
| 95 |
|
| 96 |
@app.post("/chat", response_model=ChatResponse)
|
| 97 |
-
async def chat_endpoint(
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
"""
|
| 102 |
-
result = await asyncio.to_thread(chat, request.prompt)
|
| 103 |
-
return ChatResponse(response=result)
|
| 104 |
-
|
| 105 |
-
# Create a Gradio Interface for a user-friendly web UI.
|
| 106 |
-
iface = gr.Interface(
|
| 107 |
-
fn=chat,
|
| 108 |
-
inputs="text",
|
| 109 |
-
outputs="text",
|
| 110 |
-
title="MySQL LLM Chat",
|
| 111 |
-
description="Chat interface for the MySQL finetuned model"
|
| 112 |
-
)
|
| 113 |
|
| 114 |
-
#
|
| 115 |
-
app = gr.mount_gradio_app(app, iface, path="/gradio")
|
| 116 |
-
|
| 117 |
-
# Run the server with Uvicorn if this file is executed as the main module.
|
| 118 |
if __name__ == "__main__":
|
| 119 |
import uvicorn
|
| 120 |
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
|
|
|
| 44 |
|
| 45 |
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
import os
|
|
|
|
| 48 |
from fastapi import FastAPI
|
| 49 |
from pydantic import BaseModel
|
|
|
|
| 50 |
from llama_cpp import Llama
|
| 51 |
from huggingface_hub import snapshot_download
|
| 52 |
|
| 53 |
+
# Optionally increase the HTTP timeout via an environment variable (1 hour)
|
| 54 |
+
os.environ["HF_HUB_HTTP_TIMEOUT"] = "3600"
|
| 55 |
|
| 56 |
# Download the model repository locally; this call blocks until the download completes.
|
| 57 |
local_repo = snapshot_download(
|
|
|
|
| 65 |
llm = Llama.from_pretrained(
|
| 66 |
repo_id="DebabrataHalder/mysqlmodel", # valid repo id format
|
| 67 |
filename="mysqlmodel.gguf",
|
| 68 |
+
cache_dir=local_repo, # point to the downloaded snapshot
|
| 69 |
+
n_ctx=2048, # adjust context size if needed
|
| 70 |
+
local_files_only=True # ensure only local files are used
|
| 71 |
)
|
| 72 |
|
| 73 |
+
# Define a simple function to interact with the model
|
| 74 |
def chat(prompt: str) -> str:
|
| 75 |
output = llm(prompt)
|
| 76 |
return output["choices"][0]["text"]
|
| 77 |
|
| 78 |
+
# Set up FastAPI
|
| 79 |
app = FastAPI(title="MySQL LLM Chat API")
|
| 80 |
|
| 81 |
+
# Define request and response models using Pydantic
|
| 82 |
class ChatRequest(BaseModel):
|
| 83 |
prompt: str
|
| 84 |
|
|
|
|
| 86 |
response: str
|
| 87 |
|
| 88 |
@app.post("/chat", response_model=ChatResponse)
|
| 89 |
+
async def chat_endpoint(chat_request: ChatRequest):
|
| 90 |
+
prompt = chat_request.prompt
|
| 91 |
+
response_text = chat(prompt)
|
| 92 |
+
return ChatResponse(response=response_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
+
# If running locally, use: uvicorn app:app --reload
|
|
|
|
|
|
|
|
|
|
| 95 |
if __name__ == "__main__":
|
| 96 |
import uvicorn
|
| 97 |
uvicorn.run(app, host="0.0.0.0", port=8000)
|
| 98 |
+
|