Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,17 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
-
import torch
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
)
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
# Display chat messages from history on app rerun
|
| 21 |
-
for message in st.session_state.messages:
|
| 22 |
-
with st.chat_message(message["role"]):
|
| 23 |
-
st.markdown(message["content"])
|
| 24 |
-
|
| 25 |
-
# Accept user input
|
| 26 |
-
if prompt := st.chat_input("Ask me anything about data structures in LeetCode"):
|
| 27 |
-
# Add user message to chat history
|
| 28 |
-
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 29 |
-
# Display user message in chat message container
|
| 30 |
-
with st.chat_message("user"):
|
| 31 |
-
st.markdown(prompt)
|
| 32 |
-
|
| 33 |
-
# Prepare the chat message for the model
|
| 34 |
-
messages = st.session_state.messages[-10:] # limit messages to last 10 for performance
|
| 35 |
-
text = tokenizer.apply_chat_template(
|
| 36 |
-
messages,
|
| 37 |
-
tokenize=False,
|
| 38 |
-
add_generation_prompt=True
|
| 39 |
-
)
|
| 40 |
-
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
| 41 |
-
|
| 42 |
-
# Generate response from the model
|
| 43 |
-
generated_ids = model.generate(
|
| 44 |
-
**model_inputs,
|
| 45 |
-
max_new_tokens=512
|
| 46 |
-
)
|
| 47 |
-
generated_ids = [
|
| 48 |
-
output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
|
| 49 |
-
]
|
| 50 |
-
|
| 51 |
-
# Decode the response
|
| 52 |
-
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 53 |
-
|
| 54 |
-
# Add bot response to chat history
|
| 55 |
-
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 56 |
-
|
| 57 |
-
# Display bot response in chat message container
|
| 58 |
-
with st.chat_message("assistant"):
|
| 59 |
-
st.markdown(response)
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
def main():
|
| 4 |
+
st.title("Popular Data Structures")
|
| 5 |
+
|
| 6 |
+
if st.button("What are the most popular data structures?"):
|
| 7 |
+
st.write("Here are some of the most popular data structures:")
|
| 8 |
+
st.write("- Arrays")
|
| 9 |
+
st.write("- Linked Lists")
|
| 10 |
+
st.write("- Stacks")
|
| 11 |
+
st.write("- Queues")
|
| 12 |
+
st.write("- Hash Tables")
|
| 13 |
+
st.write("- Trees (e.g., Binary Trees, BST, AVL Trees)")
|
| 14 |
+
st.write("- Graphs")
|
| 15 |
+
|
| 16 |
+
if __name__ == "__main__":
|
| 17 |
+
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|