zeeshan391 commited on
Commit
cb11302
·
verified ·
1 Parent(s): 1d020db

updated app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -59
app.py CHANGED
@@ -1,7 +1,8 @@
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
 
@@ -19,7 +20,6 @@ class StoryRequest(BaseModel):
19
  mood: str
20
  story_type: str
21
  theme: str
22
- length: int
23
  num_scenes: int
24
  txt: str
25
 
@@ -40,9 +40,19 @@ def load_model(
40
  os.chmod(final_model_path, 0o777)
41
  print("Files downloaded!")
42
 
43
- model = Llama(
 
 
 
 
 
44
  model_path=final_model_path,
45
- n_ctx=1024
 
 
 
 
 
46
  )
47
 
48
  print("Model loaded!")
@@ -51,6 +61,7 @@ def load_model(
51
 
52
  llm = load_model()
53
 
 
54
  # Create a prompt template
55
  # system = """You are a helpful and creative assistant that specializes in generating engaging and imaginative stories for kids.
56
  # Based on the user's provided mood, preferred story type, theme, age, and desired story length of 500-600 words, create a unique and captivating story.
@@ -58,71 +69,37 @@ llm = load_model()
58
  # to create another adventure soon!
59
  # """
60
 
61
- # system = """You are a helpful and creative assistant that specializes in generating engaging and imaginative short storie for kids.
62
- # Based on the user's provided mood, preferred story type, theme, age, and desired story length of 500-600 words, create a unique and captivating story.
63
- # Always start with Story Title then generate a single story.Storie begin on Page 1(also mention the all pages headings in bold) and end on Page 7.
64
- # Total pages in storie are seven each page have one short paragraph and dont ask for any feedback at the end just sign off with a cute closing inviting the reader
65
- # to create another adventure soon!
66
- # """
67
-
68
- from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
69
- from langchain_core.messages import HumanMessage
70
-
71
- prompt = ChatPromptTemplate.from_messages(
72
- [
73
- (
74
- "system",
75
- """You are a helpful and creative assistant that specializes in generating engaging and imaginative short storie for kids.
76
- Based on the user's provided mood, preferred story type, theme, age, and desired story length of 500-600 words, create a unique and captivating story.
77
- Always start with Story Title then generate a single story.Storie begin on Page 1(also mention the all pages headings in bold) and end on Page 7.
78
- Total pages in storie are seven each page have one short paragraph and dont ask for any feedback at the end just sign off with a cute closing inviting the reader
79
- to create another adventure soon!
80
- """,
81
- ),
82
- MessagesPlaceholder(variable_name="messages"),
83
- ]
84
- )
85
-
86
-
87
 
88
- # prompt_template = ChatPromptTemplate.from_messages([("system", system), ("human", "{text}")])
89
 
90
  # FastAPI endpoint to generate the story
91
  @app.post("/generate_story/")
92
  async def generate_story(story_request: StoryRequest):
93
-
94
- # chain = prompt | llm
95
-
96
  story = f"""here are the inputs from user:
97
  - **Mood:** {story_request.mood}
98
  - **Story Type:** {story_request.story_type}
99
  - **Theme:** {story_request.theme}
100
  - **Details Provided:** {story_request.txt}
101
  """
102
-
103
- # response = chain.invoke({"messages": [HumanMessage(content=story)]})
104
-
105
- formatted_prompt = prompt.format(messages=[HumanMessage(content=story)])
106
 
107
- # Extract the raw string from the `ChatPromptValue`
108
- # formatted_text = formatted_prompt.to_string()
109
-
110
- chain = prompt | llm
111
- response = chain.invoke(formatted_prompt)
112
- # response = llm(formatted_prompt)
113
-
114
- # final_prompt = prompt_template.format(text=story)
115
 
116
  # Create the LLMChain
117
  # chain = LLMChain(llm=llm, prompt=prompt_template)
118
- # chain = llm | prompt_template
119
 
120
  # try:
121
  # response = chain.invoke(final_prompt)
122
  # return {"story": response}
123
  # except Exception as e:
124
  # raise HTTPException(status_code=500, detail=str(e))
125
- # response = chain.invoke(final_prompt)
126
 
127
  if not response:
128
  raise HTTPException(status_code=500, detail="Failed to generate the story")
@@ -152,14 +129,4 @@ async def generate_story(story_request: StoryRequest):
152
  return {
153
  "story": response,
154
  "images": images
155
- }
156
-
157
-
158
-
159
- # image_prompt = (
160
- # f"Generate an image for Scene {i+1}. "
161
- # f"This image should represent the details described in paragraph {i+1} of the story. "
162
- # f"Mood: {mood}, Story Type: {', '.join(story_type)}, Theme: {theme}. "
163
- # f"Story: {response} "
164
- # f"Focus on the key elements in paragraph {i+1}."
165
- # )
 
1
  from fastapi import FastAPI, HTTPException
2
  from pydantic import BaseModel
3
+ from langchain_community.llms import LlamaCpp
4
  from huggingface_hub.file_download import http_get
5
+ # from llama_cpp import Llama
6
  from langchain_core.callbacks import CallbackManager, StreamingStdOutCallbackHandler
7
  # from langchain_core.prompts import ChatPromptTemplate
8
 
 
20
  mood: str
21
  story_type: str
22
  theme: str
 
23
  num_scenes: int
24
  txt: str
25
 
 
40
  os.chmod(final_model_path, 0o777)
41
  print("Files downloaded!")
42
 
43
+ # model = Llama(
44
+ # model_path=final_model_path,
45
+ # n_ctx=1024
46
+ # )
47
+
48
+ model = LlamaCpp(
49
  model_path=final_model_path,
50
+ temperature=0.3,
51
+ max_tokens=2000,
52
+ top_p=1,
53
+ n_ctx=1024,
54
+ callback_manager=callback_manager,
55
+ verbose=True,
56
  )
57
 
58
  print("Model loaded!")
 
61
 
62
  llm = load_model()
63
 
64
+
65
  # Create a prompt template
66
  # system = """You are a helpful and creative assistant that specializes in generating engaging and imaginative stories for kids.
67
  # Based on the user's provided mood, preferred story type, theme, age, and desired story length of 500-600 words, create a unique and captivating story.
 
69
  # to create another adventure soon!
70
  # """
71
 
72
+ system = """You are a helpful and creative assistant that specializes in generating engaging and imaginative short storie for kids.
73
+ Based on the user's provided mood, preferred story type, theme, age, and desired story length of 500-600 words, create a unique and captivating story.
74
+ Always start with Story Title then generate a single story.Storie begin on Page 1(also mention the all pages headings in bold) and end on Page 7.
75
+ Total pages in storie are seven each page have one short paragraph and dont ask for any feedback at the end just sign off with a cute closing inviting the reader
76
+ to create another adventure soon!
77
+ """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
+ prompt_template = ChatPromptTemplate.from_messages([("system", system), ("human", "{text}")])
80
 
81
  # FastAPI endpoint to generate the story
82
  @app.post("/generate_story/")
83
  async def generate_story(story_request: StoryRequest):
 
 
 
84
  story = f"""here are the inputs from user:
85
  - **Mood:** {story_request.mood}
86
  - **Story Type:** {story_request.story_type}
87
  - **Theme:** {story_request.theme}
88
  - **Details Provided:** {story_request.txt}
89
  """
 
 
 
 
90
 
91
+ final_prompt = prompt_template.format(text=story)
 
 
 
 
 
 
 
92
 
93
  # Create the LLMChain
94
  # chain = LLMChain(llm=llm, prompt=prompt_template)
95
+ chain = llm | prompt_template
96
 
97
  # try:
98
  # response = chain.invoke(final_prompt)
99
  # return {"story": response}
100
  # except Exception as e:
101
  # raise HTTPException(status_code=500, detail=str(e))
102
+ response = chain.invoke(final_prompt)
103
 
104
  if not response:
105
  raise HTTPException(status_code=500, detail="Failed to generate the story")
 
129
  return {
130
  "story": response,
131
  "images": images
132
+ }