Spaces:
Sleeping
Sleeping
updated app
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
from pydantic import BaseModel
|
| 3 |
-
from
|
| 4 |
-
from
|
| 5 |
from langchain_core.callbacks import CallbackManager, StreamingStdOutCallbackHandler
|
| 6 |
from langchain_core.prompts import ChatPromptTemplate
|
| 7 |
|
|
@@ -25,10 +25,30 @@ class StoryRequest(BaseModel):
|
|
| 25 |
# Initialize the LLM
|
| 26 |
callback_manager = CallbackManager([StreamingStdOutCallbackHandler()])
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
# Create a prompt template
|
| 34 |
# system = """You are a helpful and creative assistant that specializes in generating engaging and imaginative stories for kids.
|
|
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
from pydantic import BaseModel
|
| 3 |
+
from huggingface_hub.file_download import http_get
|
| 4 |
+
from llama_cpp import Llama
|
| 5 |
from langchain_core.callbacks import CallbackManager, StreamingStdOutCallbackHandler
|
| 6 |
from langchain_core.prompts import ChatPromptTemplate
|
| 7 |
|
|
|
|
| 25 |
# Initialize the LLM
|
| 26 |
callback_manager = CallbackManager([StreamingStdOutCallbackHandler()])
|
| 27 |
|
| 28 |
+
def load_model(
|
| 29 |
+
directory: str = ".",
|
| 30 |
+
model_name: str = "natsumura-storytelling-rp-1.0-llama-3.1-8B.Q3_K_M.gguf",
|
| 31 |
+
model_url: str = "https://huggingface.co/tohur/natsumura-storytelling-rp-1.0-llama-3.1-8b-GGUF/tree/main/natsumura-storytelling-rp-1.0-llama-3.1-8B.Q3_K_M.gguf"
|
| 32 |
+
):
|
| 33 |
+
final_model_path = os.path.join(directory, model_name)
|
| 34 |
+
|
| 35 |
+
print("Downloading all files...")
|
| 36 |
+
if not os.path.exists(final_model_path):
|
| 37 |
+
with open(final_model_path, "wb") as f:
|
| 38 |
+
http_get(model_url, f)
|
| 39 |
+
os.chmod(final_model_path, 0o777)
|
| 40 |
+
print("Files downloaded!")
|
| 41 |
+
|
| 42 |
+
model = Llama(
|
| 43 |
+
model_path=final_model_path,
|
| 44 |
+
n_ctx=1024
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
print("Model loaded!")
|
| 48 |
+
return model
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
llm = load_model()
|
| 52 |
|
| 53 |
# Create a prompt template
|
| 54 |
# system = """You are a helpful and creative assistant that specializes in generating engaging and imaginative stories for kids.
|