Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# Configuration de la page Streamlit
|
6 |
+
st.set_page_config(page_title="Assistant Mathématique", page_icon="🔢", layout="wide")
|
7 |
+
|
8 |
+
@st.cache_resource
|
9 |
+
def load_model():
|
10 |
+
"""Charge le modèle et le tokenizer (mise en cache par Streamlit)"""
|
11 |
+
model_name = "your-username/deepseek-math-tutor" # Remplacez par votre nom de modèle
|
12 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
13 |
+
model = AutoModelForCausalLM.from_pretrained(
|
14 |
+
model_name,
|
15 |
+
torch_dtype=torch.float16,
|
16 |
+
device_map="auto"
|
17 |
+
)
|
18 |
+
return model, tokenizer
|
19 |
+
|
20 |
+
def generate_response(prompt, model, tokenizer):
|
21 |
+
"""Génère une réponse à partir du prompt"""
|
22 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
23 |
+
|
24 |
+
with torch.no_grad():
|
25 |
+
outputs = model.generate(
|
26 |
+
**inputs,
|
27 |
+
max_new_tokens=1200,
|
28 |
+
temperature=0.7,
|
29 |
+
do_sample=True,
|
30 |
+
top_p=0.95,
|
31 |
+
)
|
32 |
+
|
33 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
34 |
+
return response.split("### Response:")[-1].strip()
|
35 |
+
|
36 |
+
def format_prompt(question):
|
37 |
+
"""Formate le prompt comme pendant l'entraînement"""
|
38 |
+
return f"""Below is an instruction that describes a task, paired with an input that provides further context.
|
39 |
+
Write a response that appropriately completes the request.
|
40 |
+
Before answering, think carefully about the question and create a step-by-step chain of thoughts to ensure a logical and accurate response.
|
41 |
+
Your goal is to teach maths a beginner so make it friendly and accessible. Break down your chain of thoughts as for him/her to understand.
|
42 |
+
|
43 |
+
### Instruction:
|
44 |
+
You are a maths expert with advanced knowledge in pedagogy, arithmetics, geometry, analysis, calculus.
|
45 |
+
Please answer the following questions.
|
46 |
+
|
47 |
+
### Question:
|
48 |
+
{question}
|
49 |
+
|
50 |
+
### Response:"""
|
51 |
+
|
52 |
+
def main():
|
53 |
+
# Titre de l'application
|
54 |
+
st.title("🔢 Assistant Mathématique")
|
55 |
+
st.markdown("---")
|
56 |
+
|
57 |
+
# Chargement du modèle
|
58 |
+
with st.spinner("Chargement du modèle..."):
|
59 |
+
model, tokenizer = load_model()
|
60 |
+
|
61 |
+
# Initialisation de l'historique des messages dans la session state
|
62 |
+
if "messages" not in st.session_state:
|
63 |
+
st.session_state.messages = []
|
64 |
+
|
65 |
+
# Affichage de l'historique des messages
|
66 |
+
for message in st.session_state.messages:
|
67 |
+
with st.chat_message(message["role"]):
|
68 |
+
st.markdown(message["content"])
|
69 |
+
|
70 |
+
# Zone de saisie utilisateur
|
71 |
+
if question := st.chat_input("Posez votre question mathématique..."):
|
72 |
+
# Afficher la question de l'utilisateur
|
73 |
+
with st.chat_message("user"):
|
74 |
+
st.markdown(question)
|
75 |
+
st.session_state.messages.append({"role": "user", "content": question})
|
76 |
+
|
77 |
+
# Générer et afficher la réponse
|
78 |
+
with st.chat_message("assistant"):
|
79 |
+
with st.spinner("Réflexion en cours..."):
|
80 |
+
prompt = format_prompt(question)
|
81 |
+
response = generate_response(prompt, model, tokenizer)
|
82 |
+
st.markdown(response)
|
83 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
84 |
+
|
85 |
+
# Bouton pour effacer l'historique
|
86 |
+
if st.sidebar.button("Effacer l'historique"):
|
87 |
+
st.session_state.messages = []
|
88 |
+
st.rerun()
|
89 |
+
|
90 |
+
# Informations dans la barre latérale
|
91 |
+
with st.sidebar:
|
92 |
+
st.markdown("### À propos")
|
93 |
+
st.markdown("""
|
94 |
+
Cet assistant utilise un modèle DeepSeek spécialement entraîné pour:
|
95 |
+
- Expliquer les concepts mathématiques
|
96 |
+
- Résoudre des problèmes étape par étape
|
97 |
+
- Fournir des explications claires et adaptées aux débutants
|
98 |
+
""")
|
99 |
+
|
100 |
+
if __name__ == "__main__":
|
101 |
+
main()
|