Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Titolo dell'app
|
| 6 |
+
st.title("🤖 Chatbot DeepSeek Transformers + Streamlit")
|
| 7 |
+
|
| 8 |
+
@st.cache_resource
|
| 9 |
+
def load_model():
|
| 10 |
+
model_name = "deepseek-ai/DeepSeek-Coder-V2-Instruct"
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 12 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto")
|
| 13 |
+
return tokenizer, model
|
| 14 |
+
|
| 15 |
+
tokenizer, model = load_model()
|
| 16 |
+
|
| 17 |
+
if "chat_history" not in st.session_state:
|
| 18 |
+
st.session_state.chat_history = []
|
| 19 |
+
|
| 20 |
+
user_input = st.text_input("Scrivi il tuo messaggio:")
|
| 21 |
+
|
| 22 |
+
if user_input:
|
| 23 |
+
st.session_state.chat_history.append(("🧑", user_input))
|
| 24 |
+
|
| 25 |
+
inputs = tokenizer(user_input, return_tensors="pt").to(model.device)
|
| 26 |
+
outputs = model.generate(**inputs, max_new_tokens=256, do_sample=True, temperature=0.7)
|
| 27 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 28 |
+
|
| 29 |
+
st.session_state.chat_history.append(("🤖", response))
|
| 30 |
+
|
| 31 |
+
for speaker, msg in st.session_state.chat_history:
|
| 32 |
+
st.markdown(f"**{speaker}**: {msg}")
|