nileshhanotia commited on
Commit
e007025
·
verified ·
1 Parent(s): 1b81af7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -0
app.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
+
4
+ # Load the model and tokenizer
5
+ tokenizer = AutoTokenizer.from_pretrained("HuggingFaceTB/SmolLM2-1.7B-Instruct")
6
+ model = AutoModelForCausalLM.from_pretrained("HuggingFaceTB/SmolLM2-1.7B-Instruct")
7
+
8
+ # Streamlit UI
9
+ st.title("ChatGPT Clone")
10
+ st.write("A simple chatbot interface using SmolLM2-1.7B-Instruct")
11
+
12
+ # Conversation history
13
+ if "history" not in st.session_state:
14
+ st.session_state.history = []
15
+
16
+ # User input
17
+ user_input = st.text_input("You:", key="input")
18
+
19
+ if st.button("Send") and user_input:
20
+ # Add user input to the history
21
+ st.session_state.history.append({"role": "user", "content": user_input})
22
+
23
+ # Tokenize input and generate a response
24
+ inputs = tokenizer(user_input, return_tensors="pt", padding=True, truncation=True)
25
+ outputs = model.generate(**inputs, max_length=150, do_sample=True, top_p=0.95, top_k=50)
26
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
27
+
28
+ # Add response to the history
29
+ st.session_state.history.append({"role": "bot", "content": response})
30
+
31
+ # Display conversation history
32
+ for message in st.session_state.history:
33
+ role = "User" if message["role"] == "user" else "Bot"
34
+ st.markdown(f"**{role}:** {message['content']}")
35
+
36
+ # Reset chat button
37
+ if st.button("Reset Chat"):
38
+ st.session_state.history = []