zeeshan391 commited on
Commit
3f58313
1 Parent(s): f1d41aa

updated app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -11
app.py CHANGED
@@ -3,7 +3,7 @@ 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
 
8
  import os
9
  import fal_client
@@ -58,37 +58,61 @@ 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
- prompt_template = ChatPromptTemplate.from_messages([("system", system), ("human", "{text}")])
 
 
69
 
70
  # FastAPI endpoint to generate the story
71
  @app.post("/generate_story/")
72
  async def generate_story(story_request: StoryRequest):
 
 
 
73
  story = f"""here are the inputs from user:
74
  - **Mood:** {story_request.mood}
75
  - **Story Type:** {story_request.story_type}
76
  - **Theme:** {story_request.theme}
77
  - **Details Provided:** {story_request.txt}
78
  """
 
 
79
 
80
- final_prompt = prompt_template.format(text=story)
81
 
82
  # Create the LLMChain
83
  # chain = LLMChain(llm=llm, prompt=prompt_template)
84
- chain = llm | prompt_template
85
 
86
  # try:
87
  # response = chain.invoke(final_prompt)
88
  # return {"story": response}
89
  # except Exception as e:
90
  # raise HTTPException(status_code=500, detail=str(e))
91
- response = chain.invoke(final_prompt)
92
 
93
  if not response:
94
  raise HTTPException(status_code=500, detail="Failed to generate the story")
 
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
 
8
  import os
9
  import fal_client
 
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
+
70
+ prompt = ChatPromptTemplate.from_messages(
71
+ [
72
+ (
73
+ "system",
74
+ """You are a helpful and creative assistant that specializes in generating engaging and imaginative short storie for kids.
75
+ 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.
76
+ 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.
77
+ 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
78
+ to create another adventure soon!
79
+ """,
80
+ ),
81
+ MessagesPlaceholder(variable_name="messages"),
82
+ ]
83
+ )
84
 
85
+
86
+
87
+ # prompt_template = ChatPromptTemplate.from_messages([("system", system), ("human", "{text}")])
88
 
89
  # FastAPI endpoint to generate the story
90
  @app.post("/generate_story/")
91
  async def generate_story(story_request: StoryRequest):
92
+
93
+ chain = prompt | llm
94
+
95
  story = f"""here are the inputs from user:
96
  - **Mood:** {story_request.mood}
97
  - **Story Type:** {story_request.story_type}
98
  - **Theme:** {story_request.theme}
99
  - **Details Provided:** {story_request.txt}
100
  """
101
+
102
+ response = chain.invoke({"messages": [HumanMessage(content=story)]})
103
 
104
+ # final_prompt = prompt_template.format(text=story)
105
 
106
  # Create the LLMChain
107
  # chain = LLMChain(llm=llm, prompt=prompt_template)
108
+ # chain = llm | prompt_template
109
 
110
  # try:
111
  # response = chain.invoke(final_prompt)
112
  # return {"story": response}
113
  # except Exception as e:
114
  # raise HTTPException(status_code=500, detail=str(e))
115
+ # response = chain.invoke(final_prompt)
116
 
117
  if not response:
118
  raise HTTPException(status_code=500, detail="Failed to generate the story")