Spaces:
Sleeping
Sleeping
LijinDurairaj
commited on
Commit
•
1fb5bb2
1
Parent(s):
69fd42d
creating chatbot which can get you the best movie quotes
Browse files- app.py +107 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#packages
|
2 |
+
import streamlit as st
|
3 |
+
from langchain.prompts import PromptTemplate
|
4 |
+
from langchain.chains import LLMChain
|
5 |
+
from langchain.llms import (
|
6 |
+
HuggingFacePipeline, HuggingFaceEndpoint
|
7 |
+
)
|
8 |
+
from langchain.prompts.chat import(
|
9 |
+
ChatPromptTemplate,
|
10 |
+
SystemMessagePromptTemplate,
|
11 |
+
HumanMessagePromptTemplate
|
12 |
+
)
|
13 |
+
|
14 |
+
|
15 |
+
# intiaiizing variables
|
16 |
+
model_id = "mistralai/Mixtral-8x7B-Instruct-v0.1"
|
17 |
+
|
18 |
+
|
19 |
+
few_shot_example='''
|
20 |
+
Example 1:
|
21 |
+
User: get me best quotes from the movie The Wizard of Oz
|
22 |
+
Assistant: sure!
|
23 |
+
1. Toto, I've a feeling we're not in Kansas anymore.
|
24 |
+
2. And remember, my sentimental friend, that a heart is not judged by how much *you* love; but by how much you are loved by others.
|
25 |
+
3. Pay no attention to that man behind the curtain.
|
26 |
+
|
27 |
+
Example 2:
|
28 |
+
User: get the best quotes from the movie pursuit of happiness
|
29 |
+
Assistant: sure!
|
30 |
+
1. Don't ever let someone tell you that you can't do something. Not even me. You got a dream, you gotta protect it. When people can't do something themselves, they're gonna tell you that you can't do it. You want something, go get it. Period.
|
31 |
+
2. Others may question your credentials, your papers, your degrees. But what is inside you no one can take from you or tarnish"
|
32 |
+
|
33 |
+
Example 3:
|
34 |
+
User: get the best quotes from the movie blha blah
|
35 |
+
Assistant: Sorry, I dont know
|
36 |
+
|
37 |
+
Now, continue:
|
38 |
+
'''
|
39 |
+
#functions
|
40 |
+
def load_prompt_chain():
|
41 |
+
try:
|
42 |
+
|
43 |
+
llm = HuggingFaceEndpoint(
|
44 |
+
repo_id=model_id, max_length=2000, temperature=0.5
|
45 |
+
)
|
46 |
+
|
47 |
+
system_message_prompt=SystemMessagePromptTemplate.from_template(template=
|
48 |
+
'''you are an excellent Assistant AI expert on getting Quotes from hollywood movies, please get the maximum 5 best quotes from the movie {movie_name}, dont assume or hallucinate answers, if you dont know the answer, just say you dont know.
|
49 |
+
\n\n {few_shot_example}''',
|
50 |
+
)
|
51 |
+
speaker_message_prompt=HumanMessagePromptTemplate.from_template(
|
52 |
+
'get the best quotes from the movie {movie_name}'
|
53 |
+
)
|
54 |
+
chat_prompt=ChatPromptTemplate.from_messages([
|
55 |
+
system_message_prompt,speaker_message_prompt
|
56 |
+
])
|
57 |
+
|
58 |
+
chain=LLMChain(llm=llm,prompt=chat_prompt)
|
59 |
+
|
60 |
+
return chain
|
61 |
+
except Exception as ex:
|
62 |
+
st.error("Error loading prompt chain.")
|
63 |
+
st.error(str(ex))
|
64 |
+
return None
|
65 |
+
|
66 |
+
chain = load_prompt_chain()
|
67 |
+
|
68 |
+
#session states
|
69 |
+
if 'movie_name' not in st.session_state:
|
70 |
+
st.session_state['movie_name']=''
|
71 |
+
|
72 |
+
if 'movie_quotes' not in st.session_state:
|
73 |
+
st.session_state['movie_quotes']=''
|
74 |
+
|
75 |
+
# if 'selected_model' not in st.session_state:
|
76 |
+
# st.session_state['selected_model']=''
|
77 |
+
|
78 |
+
#page controls
|
79 |
+
st.title('best movie quotes.')
|
80 |
+
col1, col2=st.columns([3,9])
|
81 |
+
with st.form('movie_quotes_form'):
|
82 |
+
|
83 |
+
# with col1:
|
84 |
+
# st.session_state['selected_model']=st.selectbox(
|
85 |
+
# 'please select a model',
|
86 |
+
# ('mistralai/Mixtral-8x7B-Instruct-v0.1','meta-llama/Llama-2-7b-chat-hf')
|
87 |
+
# )
|
88 |
+
|
89 |
+
# with col2:
|
90 |
+
markdown=st.markdown(st.session_state['movie_quotes'],unsafe_allow_html=True)
|
91 |
+
st.divider()
|
92 |
+
st.session_state['movie_name']=st.text_input(label='enter the movie name',
|
93 |
+
value=st.session_state['movie_name'])
|
94 |
+
|
95 |
+
submitted=st.form_submit_button(label='get quotes')
|
96 |
+
if submitted and chain:
|
97 |
+
try:
|
98 |
+
message=chain.run({
|
99 |
+
'few_shot_example':few_shot_example,
|
100 |
+
'movie_name':st.session_state['movie_name']
|
101 |
+
})
|
102 |
+
st.session_state['movie_quotes']+=message
|
103 |
+
markdown.write(st.session_state['movie_quotes'])
|
104 |
+
except Exception as ex:
|
105 |
+
st.error("Error while generating quotes.")
|
106 |
+
st.error(ex)
|
107 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
langchain_community
|
3 |
+
langchain
|