Spaces:
Sleeping
Sleeping
Sharathhebbar24
commited on
Commit
•
d75759d
1
Parent(s):
b3c1e52
Upload 2 files
Browse files- main.py +107 -0
- requirements.txt +3 -0
main.py
ADDED
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import streamlit as st
|
3 |
+
from langchain.llms import HuggingFaceHub
|
4 |
+
from langchain.chains import LLMChain
|
5 |
+
from langchain.prompts import PromptTemplate
|
6 |
+
|
7 |
+
from models import llms
|
8 |
+
|
9 |
+
class UserInterface():
|
10 |
+
|
11 |
+
def __init__(self, ):
|
12 |
+
st.warning("Warning: Some models may not work and some models may require GPU to run")
|
13 |
+
st.text("An Open Source Chat Application")
|
14 |
+
st.header("Open LLMs")
|
15 |
+
|
16 |
+
self.API_KEY = st.sidebar.text_input(
|
17 |
+
'API Key',
|
18 |
+
type='password',
|
19 |
+
help="Type in your HuggingFace API key to use this app"
|
20 |
+
)
|
21 |
+
|
22 |
+
models_name = (
|
23 |
+
"HuggingFaceH4/zephyr-7b-beta",
|
24 |
+
"Open-Orca/Mistral-7B-OpenOrca",
|
25 |
+
)
|
26 |
+
self.models = st.sidebar.selectbox(
|
27 |
+
label="Choose your models",
|
28 |
+
options=models_name,
|
29 |
+
help="Choose your model",
|
30 |
+
)
|
31 |
+
|
32 |
+
self.temperature = st.sidebar.slider(
|
33 |
+
label='Temperature',
|
34 |
+
min_value=0.1,
|
35 |
+
max_value=1.0,
|
36 |
+
step=0.1,
|
37 |
+
value=0.5,
|
38 |
+
help="Set the temperature to get accurate or random result"
|
39 |
+
)
|
40 |
+
|
41 |
+
self.max_token_length = st.sidebar.slider(
|
42 |
+
label="Token Length",
|
43 |
+
min_value=32,
|
44 |
+
max_value=2048,
|
45 |
+
step=16,
|
46 |
+
value=64,
|
47 |
+
help="Set max tokens to generate maximum amount of text output"
|
48 |
+
)
|
49 |
+
|
50 |
+
|
51 |
+
self.model_kwargs = {
|
52 |
+
"temperature": self.temperature,
|
53 |
+
"max_length": self.max_token_length
|
54 |
+
}
|
55 |
+
|
56 |
+
os.environ['HUGGINGFACEHUB_API_TOKEN'] = self.API_KEY
|
57 |
+
|
58 |
+
|
59 |
+
def form_data(self):
|
60 |
+
|
61 |
+
try:
|
62 |
+
if not self.API_KEY.startswith('hf_'):
|
63 |
+
st.warning('Please enter your API key!', icon='⚠')
|
64 |
+
text_input_visibility = True
|
65 |
+
|
66 |
+
|
67 |
+
st.subheader("Context")
|
68 |
+
context = st.chat_input(disabled=text_input_visibility)
|
69 |
+
st.subheader("Question")
|
70 |
+
question = st.chat_input(disabled=text_input_visibility)
|
71 |
+
|
72 |
+
|
73 |
+
template = """
|
74 |
+
Answer the question based on the context, if you don't know then output "Out of Context"
|
75 |
+
Context: {context}
|
76 |
+
Question: {question}
|
77 |
+
|
78 |
+
Answer:
|
79 |
+
"""
|
80 |
+
prompt = PromptTemplate(
|
81 |
+
template=template,
|
82 |
+
input_variables=[
|
83 |
+
'question',
|
84 |
+
'context'
|
85 |
+
]
|
86 |
+
)
|
87 |
+
llm = HuggingFaceHub(
|
88 |
+
repo_id = self.model_name,
|
89 |
+
model_kwargs = self.model_kwargs
|
90 |
+
)
|
91 |
+
|
92 |
+
llm_chain = LLMChain(
|
93 |
+
prompt=prompt,
|
94 |
+
llm=llm,
|
95 |
+
)
|
96 |
+
|
97 |
+
result = llm_chain.run({
|
98 |
+
"question": question,
|
99 |
+
"context": context
|
100 |
+
})
|
101 |
+
|
102 |
+
st.markdown(result)
|
103 |
+
except Exception as e:
|
104 |
+
st.error(e, icon="🚨")
|
105 |
+
|
106 |
+
model = UserInterface()
|
107 |
+
model.form_data()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
langchain
|
2 |
+
huggingface_hub
|
3 |
+
Streamlit
|