zeeshan391 commited on
Commit
d927270
·
verified ·
1 Parent(s): 8d87399

updated app

Browse files
Files changed (1) hide show
  1. app.py +26 -6
app.py CHANGED
@@ -1,7 +1,7 @@
1
  from fastapi import FastAPI, HTTPException
2
  from pydantic import BaseModel
3
- from langchain_huggingface.llms import HuggingFacePipeline
4
- from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
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
- tokenizer = AutoTokenizer.from_pretrained("tohur/natsumura-storytelling-rp-1.0-llama-3.1-8b")
29
- model = AutoModelForCausalLM.from_pretrained("tohur/natsumura-storytelling-rp-1.0-llama-3.1-8b")
30
- pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, max_new_tokens=2000)
31
- llm = HuggingFacePipeline(pipeline=pipe)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.