Spaces:
Sleeping
Sleeping
kursat commited on
Commit ·
b555c2f
1
Parent(s): 2591e83
app
Browse files- app.py +42 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Load the model and tokenizer
|
| 6 |
+
model_name = "kursathalat/gemma-pragma"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 9 |
+
|
| 10 |
+
# Streamlit app
|
| 11 |
+
st.title("Hugging Face Chatbot")
|
| 12 |
+
|
| 13 |
+
# Initialize the session state for history if it doesn't exist
|
| 14 |
+
if "history" not in st.session_state:
|
| 15 |
+
st.session_state.history = []
|
| 16 |
+
|
| 17 |
+
# User input
|
| 18 |
+
user_input = st.text_input("You:", "")
|
| 19 |
+
|
| 20 |
+
# Respond to user input
|
| 21 |
+
if user_input:
|
| 22 |
+
# Tokenize input
|
| 23 |
+
inputs = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors="pt")
|
| 24 |
+
|
| 25 |
+
# Get the bot's response
|
| 26 |
+
if len(st.session_state.history) > 0:
|
| 27 |
+
past_inputs = tokenizer.encode(" ".join(st.session_state.history) + tokenizer.eos_token, return_tensors="pt")
|
| 28 |
+
inputs = torch.cat([past_inputs, inputs], dim=-1)
|
| 29 |
+
|
| 30 |
+
outputs = model.generate(inputs, max_length=1000, pad_token_id=tokenizer.eos_token_id)
|
| 31 |
+
response = tokenizer.decode(outputs[:, inputs.shape[-1]:][0], skip_special_tokens=True)
|
| 32 |
+
|
| 33 |
+
# Update chat history
|
| 34 |
+
st.session_state.history.append(user_input)
|
| 35 |
+
st.session_state.history.append(response)
|
| 36 |
+
|
| 37 |
+
# Display chat history
|
| 38 |
+
for i, msg in enumerate(st.session_state.history):
|
| 39 |
+
if i % 2 == 0:
|
| 40 |
+
st.write(f"You: {msg}")
|
| 41 |
+
else:
|
| 42 |
+
st.write(f"Bot: {msg}")
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|