Mahavaury2 commited on
Commit
5f14f54
·
verified ·
1 Parent(s): 29ac499

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -25
app.py CHANGED
@@ -1,40 +1,61 @@
1
  import gradio as gr
2
- from transformers import AutoTokenizer, AutoModelForCausalLM
3
  import torch
 
4
 
5
- checkpoint = "mistralai/Mistral-7B-Instruct-v0.3"
6
-
7
- # Download tokenizer & model
8
- tokenizer = AutoTokenizer.from_pretrained(checkpoint, trust_remote_code=True)
9
- model = AutoModelForCausalLM.from_pretrained(
10
- checkpoint,
11
- device_map="auto", # or "cpu" / "cuda"
12
- trust_remote_code=True
13
- )
14
- # (Optional) set model to inference mode, etc.
15
- # model.eval()
16
-
17
- def inference_fn(prompt):
18
- # Tokenize
19
- inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
20
- # Generate
21
- output_tokens = model.generate(**inputs, max_new_tokens=128)
22
- # Decode
23
- return tokenizer.decode(output_tokens[0], skip_special_tokens=True)
24
-
25
- # Pastel gradient CSS
26
  css = """
27
  .gradio-container {
28
  background: linear-gradient(to right, #FFDEE9, #B5FFFC);
29
  }
30
  """
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  with gr.Blocks(css=css) as demo:
33
- gr.Markdown("<h1 style='text-align: center;'>Bonjour Dans le chat du consentement</h1>")
34
  user_input = gr.Textbox(label="Entrez votre message ici:", lines=3)
35
  output = gr.Textbox(label="Réponse du Modèle:", lines=5)
36
  send_button = gr.Button("Envoyer")
37
 
38
- send_button.click(fn=inference_fn, inputs=user_input, outputs=output)
 
39
 
40
- demo.launch()
 
 
 
1
  import gradio as gr
 
2
  import torch
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
4
 
5
+ # 1) Define pastel gradient CSS
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  css = """
7
  .gradio-container {
8
  background: linear-gradient(to right, #FFDEE9, #B5FFFC);
9
  }
10
  """
11
 
12
+ title = "Bonjour Dans le chat du consentement"
13
+
14
+ # 2) Load the Mistral model & tokenizer from HF Hub
15
+ model_id = "mistralai/Mistral-7B-Instruct-v0.3"
16
+
17
+ # If you're on a GPU Space, you can do:
18
+ # device_map = "auto"
19
+ # torch_dtype = torch.bfloat16
20
+ # If you're on a CPU-only Space, remove those arguments or set device_map="cpu"
21
+ tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
22
+ model = AutoModelForCausalLM.from_pretrained(
23
+ model_id,
24
+ device_map="auto", # "auto" if you have GPU
25
+ torch_dtype=torch.bfloat16, # for GPU. Remove or use float32 on CPU
26
+ trust_remote_code=True
27
+ )
28
+
29
+ # 3) Create a text-generation pipeline
30
+ generate_text = pipeline(
31
+ "text-generation",
32
+ model=model,
33
+ tokenizer=tokenizer,
34
+ max_length=512, # adjust as needed
35
+ temperature=0.7, # adjust as needed
36
+ do_sample=True
37
+ )
38
+
39
+ def mistral_inference(prompt):
40
+ """
41
+ Passes user prompt to the pipeline and returns the generated text.
42
+ We'll strip any special tokens and limit the output.
43
+ """
44
+ # The pipeline returns a list of dicts [{"generated_text": "..."}]
45
+ outputs = generate_text(prompt)
46
+ text_out = outputs[0]["generated_text"]
47
+ return text_out
48
+
49
+ # 4) Build the Gradio interface with a pastel background & greeting
50
  with gr.Blocks(css=css) as demo:
51
+ gr.Markdown(f"<h1 style='text-align:center;'>{title}</h1>")
52
  user_input = gr.Textbox(label="Entrez votre message ici:", lines=3)
53
  output = gr.Textbox(label="Réponse du Modèle:", lines=5)
54
  send_button = gr.Button("Envoyer")
55
 
56
+ # Link the button to the inference function
57
+ send_button.click(fn=mistral_inference, inputs=user_input, outputs=output)
58
 
59
+ # 5) Launch the app
60
+ if __name__ == "__main__":
61
+ demo.launch()