Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,93 +1,40 @@
|
|
1 |
-
# app.py
|
2 |
-
|
3 |
import streamlit as st
|
4 |
-
from
|
5 |
-
import
|
|
|
6 |
import os
|
7 |
-
from dotenv import load_dotenv
|
8 |
-
|
9 |
-
# Load environment variables
|
10 |
-
load_dotenv()
|
11 |
-
|
12 |
-
# Retrieve Hugging Face API token from environment variables
|
13 |
-
HF_API_TOKEN = os.getenv("HF_API_TOKEN")
|
14 |
-
|
15 |
-
# Streamlit app setup
|
16 |
-
st.title('Llama2 Chatbot Deployment on Hugging Face Spaces')
|
17 |
-
st.write("This chatbot is powered by the Llama2 model. Ask me anything!")
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
""
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
#
|
38 |
-
|
39 |
-
|
40 |
-
#
|
41 |
-
|
42 |
-
st.session_state.conversation = []
|
43 |
|
44 |
# User input
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
if message["role"] == "user":
|
55 |
-
conversation_text += f"User: {message['content']}\n"
|
56 |
-
elif message["role"] == "assistant":
|
57 |
-
conversation_text += f"Assistant: {message['content']}\n"
|
58 |
-
|
59 |
-
# Encode the input
|
60 |
-
inputs = tokenizer.encode(conversation_text + "Assistant:", return_tensors="pt").to(model.device)
|
61 |
-
|
62 |
-
# Generate a response
|
63 |
-
output = model.generate(
|
64 |
-
inputs,
|
65 |
-
max_length=1000,
|
66 |
-
temperature=0.7,
|
67 |
-
top_p=0.9,
|
68 |
-
do_sample=True,
|
69 |
-
eos_token_id=tokenizer.eos_token_id,
|
70 |
-
pad_token_id=tokenizer.eos_token_id # To avoid warnings
|
71 |
-
)
|
72 |
-
|
73 |
-
# Decode the response
|
74 |
-
response = tokenizer.decode(output[0], skip_special_tokens=True)
|
75 |
-
|
76 |
-
# Extract the assistant's reply
|
77 |
-
assistant_reply = response[len(conversation_text + "Assistant: "):].strip()
|
78 |
-
|
79 |
-
# Append the assistant's reply to the conversation history
|
80 |
-
st.session_state.conversation.append({"role": "assistant", "content": assistant_reply})
|
81 |
-
|
82 |
-
# Display the updated conversation
|
83 |
-
conversation_display = ""
|
84 |
-
for message in st.session_state.conversation:
|
85 |
-
if message["role"] == "user":
|
86 |
-
conversation_display += f"**You:** {message['content']}\n\n"
|
87 |
-
elif message["role"] == "assistant":
|
88 |
-
conversation_display += f"**Bot:** {message['content']}\n\n"
|
89 |
-
|
90 |
-
st.markdown(conversation_display)
|
91 |
-
|
92 |
-
except Exception as e:
|
93 |
-
st.error(f"An error occurred: {e}")
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from langchain import LLMChain
|
3 |
+
from langchain.chat_models import HuggingFaceHub
|
4 |
+
from langchain.prompts import ChatPromptTemplate
|
5 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
+
# Initialize HuggingFaceHub LLM with access token from environment variables
|
8 |
+
llm = HuggingFaceHub(
|
9 |
+
repo_id="meta-llama/Llama-2-7b-chat-hf",
|
10 |
+
huggingfacehub_api_token=os.getenv("HUGGINGFACE_API_KEY"),
|
11 |
+
model_kwargs={
|
12 |
+
"temperature": 0.7,
|
13 |
+
"max_new_tokens": 512,
|
14 |
+
}
|
15 |
+
)
|
16 |
+
|
17 |
+
# Define the prompt template
|
18 |
+
prompt = ChatPromptTemplate.from_messages(
|
19 |
+
[
|
20 |
+
("system", "You are a helpful assistant."),
|
21 |
+
("user", "Question: {question}")
|
22 |
+
]
|
23 |
+
)
|
24 |
+
|
25 |
+
# Create the LLM Chain
|
26 |
+
chain = LLMChain(llm=llm, prompt=prompt, output_key="response")
|
27 |
+
|
28 |
+
# Streamlit App Interface
|
29 |
+
st.title('LangChain Demo with LLaMA 2 on Hugging Face')
|
|
|
30 |
|
31 |
# User input
|
32 |
+
input_text = st.text_input("Enter your question:")
|
33 |
+
|
34 |
+
# Display the response
|
35 |
+
if input_text:
|
36 |
+
try:
|
37 |
+
response = chain.run({"question": input_text})
|
38 |
+
st.write(response)
|
39 |
+
except Exception as e:
|
40 |
+
st.error(f"Error: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|