Spaces:
Sleeping
Sleeping
Neslihan Bisgin
commited on
Commit
·
c85e599
1
Parent(s):
1aa530a
Add application file
Browse files
app.py
CHANGED
@@ -1,4 +1,37 @@
|
|
1 |
import streamlit as st
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
import torch
|
4 |
|
5 |
+
# Load the model and tokenizer
|
6 |
+
model_name = "m3rg-iitd/llamat-3-chat" #"gpt2" # You can replace this with any model of your choice
|
7 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
+
|
10 |
+
st.title("Chatbot with LlaMat")
|
11 |
+
st.write("Ask me anything about material!")
|
12 |
+
|
13 |
+
# Initialize session state for chat history
|
14 |
+
if "messages" not in st.session_state:
|
15 |
+
st.session_state.messages = []
|
16 |
+
|
17 |
+
# Function to generate response
|
18 |
+
def generate_response(prompt):
|
19 |
+
inputs = tokenizer.encode(prompt, return_tensors="pt")
|
20 |
+
outputs = model.generate(inputs, max_length=100, num_return_sequences=1)
|
21 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
22 |
+
return response
|
23 |
+
|
24 |
+
# User input
|
25 |
+
user_input = st.text_input("You: ", "")
|
26 |
+
|
27 |
+
if user_input:
|
28 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
29 |
+
response = generate_response(user_input)
|
30 |
+
st.session_state.messages.append({"role": "bot", "content": response})
|
31 |
+
|
32 |
+
# Display chat history
|
33 |
+
for message in st.session_state.messages:
|
34 |
+
if message["role"] == "user":
|
35 |
+
st.write(f"You: {message['content']}")
|
36 |
+
else:
|
37 |
+
st.write(f"Bot: {message['content']}")
|