MTNQLN commited on
Commit
31aebd2
·
verified ·
1 Parent(s): 4ae5963

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -59
app.py CHANGED
@@ -1,68 +1,56 @@
1
- import gradio as gr
2
- from huggingface_hub import InferenceClient
3
  from huggingface_hub import login
 
 
 
4
 
5
- # # Authentification avec Hugging Face
6
- # login(token=os.getenv('API_KEY'))
7
- # print(os.getenv('API_KEY'))
8
-
9
- """
10
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
11
- """
12
- client = InferenceClient("mistralai/mathstral-7B-v0.1")
13
-
14
-
15
- def respond(
16
- message,
17
- history: list[tuple[str, str]],
18
- system_message,
19
- max_tokens,
20
- temperature,
21
- top_p,
22
- ):
23
- messages = [{"role": "system", "content": system_message}]
24
-
25
- for val in history:
26
- if val[0]:
27
- messages.append({"role": "user", "content": val[0]})
28
- if val[1]:
29
- messages.append({"role": "assistant", "content": val[1]})
30
-
31
- messages.append({"role": "user", "content": message})
32
 
33
- response = ""
34
 
35
- for message in client.chat_completion(
36
- messages,
37
- max_tokens=max_tokens,
38
- stream=True,
39
- temperature=temperature,
40
- top_p=top_p,
41
- ):
42
- token = message.choices[0].delta.content
43
 
44
- response += token
45
- yield response
 
 
 
 
 
46
 
47
- """
48
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
49
- """
50
- demo = gr.ChatInterface(
51
- respond,
52
- additional_inputs=[
53
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
54
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
55
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
56
- gr.Slider(
57
- minimum=0.1,
58
- maximum=1.0,
59
- value=0.95,
60
- step=0.05,
61
- label="Top-p (nucleus sampling)",
62
- ),
63
- ],
64
  )
65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
- if __name__ == "__main__":
68
- demo.launch()
 
 
 
1
  from huggingface_hub import login
2
+ import torch
3
+ import gradio as gr
4
+ from transformers import AutoModelForCausalLM, AutoTokenizer
5
 
6
+ # Authentification avec Hugging Face
7
+ login(token=os.getenv('API_KEY'))
8
+ print(os.getenv('API_KEY'))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
 
10
 
11
+ # Charger le modèle et le tokenizer
12
+ model_name = "EleutherAI/mathstral-7b-v1" # Remplacez par le nom exact du modèle Mathstral
13
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
 
 
 
 
 
14
 
15
+ # Charger le modèle avec quantification dynamique
16
+ model = AutoModelForCausalLM.from_pretrained(
17
+ model_name,
18
+ torch_dtype=torch.float16,
19
+ device_map="auto",
20
+ low_cpu_mem_usage=True,
21
+ )
22
 
23
+ # Appliquer la quantification dynamique
24
+ model = torch.quantization.quantize_dynamic(
25
+ model, {torch.nn.Linear}, dtype=torch.qint8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  )
27
 
28
+ # Fonction pour générer du texte
29
+ def generate_text(prompt, max_new_tokens=50):
30
+ inputs = tokenizer(prompt, return_tensors="pt")
31
+ with torch.no_grad():
32
+ outputs = model.generate(
33
+ **inputs,
34
+ max_new_tokens=max_new_tokens,
35
+ do_sample=True,
36
+ top_p=0.95,
37
+ temperature=0.7
38
+ )
39
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
40
+
41
+ # Interface Gradio
42
+ def gradio_interface(prompt):
43
+ response = generate_text(prompt)
44
+ return response
45
+
46
+ # Création de l'interface Gradio
47
+ iface = gr.Interface(
48
+ fn=gradio_interface,
49
+ inputs=gr.Textbox(lines=2, placeholder="Entrez votre question mathématique ici..."),
50
+ outputs="text",
51
+ title="Assistant Mathématique (Mathstral-7B)",
52
+ description="Posez une question mathématique, et l'assistant vous répondra.",
53
+ )
54
 
55
+ # Lancer l'interface
56
+ iface.launch()