Spaces:
Runtime error
Runtime error
Commit
·
98df2e3
1
Parent(s):
9ece64d
Update main.py
Browse files
main.py
CHANGED
|
@@ -4,7 +4,6 @@ from langchain.llms import HuggingFaceHub
|
|
| 4 |
from langchain.chains import LLMChain
|
| 5 |
from langchain.prompts import PromptTemplate
|
| 6 |
|
| 7 |
-
|
| 8 |
class UserInterface():
|
| 9 |
|
| 10 |
def __init__(self, ):
|
|
@@ -12,11 +11,11 @@ class UserInterface():
|
|
| 12 |
st.text("An Open Source Chat Application")
|
| 13 |
st.header("Open LLMs")
|
| 14 |
|
| 15 |
-
self.API_KEY = st.sidebar.text_input(
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
)
|
| 20 |
|
| 21 |
models_name = (
|
| 22 |
"HuggingFaceH4/zephyr-7b-beta",
|
|
@@ -34,7 +33,7 @@ class UserInterface():
|
|
| 34 |
max_value=1.0,
|
| 35 |
step=0.1,
|
| 36 |
value=0.5,
|
| 37 |
-
help="Set the temperature to get
|
| 38 |
)
|
| 39 |
|
| 40 |
self.max_token_length = st.sidebar.slider(
|
|
@@ -43,7 +42,7 @@ class UserInterface():
|
|
| 43 |
max_value=2048,
|
| 44 |
step=16,
|
| 45 |
value=64,
|
| 46 |
-
help="Set max tokens to generate
|
| 47 |
)
|
| 48 |
|
| 49 |
|
|
@@ -52,7 +51,7 @@ class UserInterface():
|
|
| 52 |
"max_length": self.max_token_length
|
| 53 |
}
|
| 54 |
|
| 55 |
-
os.environ['HUGGINGFACEHUB_API_TOKEN'] = self.API_KEY
|
| 56 |
|
| 57 |
|
| 58 |
def form_data(self):
|
|
@@ -61,18 +60,29 @@ class UserInterface():
|
|
| 61 |
if not self.API_KEY.startswith('hf_'):
|
| 62 |
st.warning('Please enter your API key!', icon='⚠')
|
| 63 |
text_input_visibility = True
|
|
|
|
|
|
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
st.
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
question = st.
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
|
| 77 |
|
| 78 |
template = """
|
|
@@ -94,17 +104,41 @@ class UserInterface():
|
|
| 94 |
model_kwargs = self.model_kwargs
|
| 95 |
)
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
-
st.markdown(result)
|
| 108 |
except Exception as e:
|
| 109 |
st.error(e, icon="🚨")
|
| 110 |
|
|
|
|
| 4 |
from langchain.chains import LLMChain
|
| 5 |
from langchain.prompts import PromptTemplate
|
| 6 |
|
|
|
|
| 7 |
class UserInterface():
|
| 8 |
|
| 9 |
def __init__(self, ):
|
|
|
|
| 11 |
st.text("An Open Source Chat Application")
|
| 12 |
st.header("Open LLMs")
|
| 13 |
|
| 14 |
+
# self.API_KEY = st.sidebar.text_input(
|
| 15 |
+
# 'API Key',
|
| 16 |
+
# type='password',
|
| 17 |
+
# help="Type in your HuggingFace API key to use this app"
|
| 18 |
+
# )
|
| 19 |
|
| 20 |
models_name = (
|
| 21 |
"HuggingFaceH4/zephyr-7b-beta",
|
|
|
|
| 33 |
max_value=1.0,
|
| 34 |
step=0.1,
|
| 35 |
value=0.5,
|
| 36 |
+
help="Set the temperature to get accurate or random result"
|
| 37 |
)
|
| 38 |
|
| 39 |
self.max_token_length = st.sidebar.slider(
|
|
|
|
| 42 |
max_value=2048,
|
| 43 |
step=16,
|
| 44 |
value=64,
|
| 45 |
+
help="Set max tokens to generate maximum amount of text output"
|
| 46 |
)
|
| 47 |
|
| 48 |
|
|
|
|
| 51 |
"max_length": self.max_token_length
|
| 52 |
}
|
| 53 |
|
| 54 |
+
# os.environ['HUGGINGFACEHUB_API_TOKEN'] = self.API_KEY/
|
| 55 |
|
| 56 |
|
| 57 |
def form_data(self):
|
|
|
|
| 60 |
if not self.API_KEY.startswith('hf_'):
|
| 61 |
st.warning('Please enter your API key!', icon='⚠')
|
| 62 |
text_input_visibility = True
|
| 63 |
+
else:
|
| 64 |
+
text_input_visibility = False
|
| 65 |
|
| 66 |
+
|
| 67 |
+
if "messages" not in st.session_state:
|
| 68 |
+
st.session_state.messages = []
|
| 69 |
+
|
| 70 |
+
st.write(f"You are using {self.models} model")
|
| 71 |
+
|
| 72 |
+
for message in st.session_state.messages:
|
| 73 |
+
with st.chat_message(message.get('role')):
|
| 74 |
+
st.write(message.get("content"))
|
| 75 |
|
| 76 |
+
context = st.sidebar.text_input(
|
| 77 |
+
label="Context",
|
| 78 |
+
help="Context lets you know on what the answer should be generated"
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
question = st.chat_input(
|
| 83 |
+
key="question",
|
| 84 |
+
disabled=text_input_visibility
|
| 85 |
+
)
|
| 86 |
|
| 87 |
|
| 88 |
template = """
|
|
|
|
| 104 |
model_kwargs = self.model_kwargs
|
| 105 |
)
|
| 106 |
|
| 107 |
+
if question:
|
| 108 |
+
llm_chain = LLMChain(
|
| 109 |
+
prompt=prompt,
|
| 110 |
+
llm=llm,
|
| 111 |
+
)
|
| 112 |
|
| 113 |
+
result = llm_chain.run({
|
| 114 |
+
"question": question,
|
| 115 |
+
"context": context
|
| 116 |
+
})
|
| 117 |
+
|
| 118 |
+
if "Out of Context" in result:
|
| 119 |
+
result = "Out of Context"
|
| 120 |
+
st.session_state.messages.append(
|
| 121 |
+
{
|
| 122 |
+
"role":"user",
|
| 123 |
+
"content": f"Context: {context}\n\nQuestion: {question}"
|
| 124 |
+
}
|
| 125 |
+
)
|
| 126 |
+
with st.chat_message("user"):
|
| 127 |
+
st.write(f"Context: {context}\n\nQuestion: {question}")
|
| 128 |
+
|
| 129 |
+
if question.lower() == "clear":
|
| 130 |
+
del st.session_state.messages
|
| 131 |
+
return
|
| 132 |
+
|
| 133 |
+
st.session_state.messages.append(
|
| 134 |
+
{
|
| 135 |
+
"role": "assistant",
|
| 136 |
+
"content": result
|
| 137 |
+
}
|
| 138 |
+
)
|
| 139 |
+
with st.chat_message('assistant'):
|
| 140 |
+
st.markdown(result)
|
| 141 |
|
|
|
|
| 142 |
except Exception as e:
|
| 143 |
st.error(e, icon="🚨")
|
| 144 |
|