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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -23
app.py CHANGED
@@ -1,37 +1,40 @@
1
  import gradio as gr
 
 
2
 
3
- # Custom CSS for pastel gradient
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  css = """
5
  .gradio-container {
6
  background: linear-gradient(to right, #FFDEE9, #B5FFFC);
7
  }
8
  """
9
 
10
- # Load the Mistral-7B-Instruct-v0.3 model via Gradio's load function
11
- model = gr.load("models/mistralai/Mistral-7B-Instruct-v0.3")
12
-
13
- def inference_fn(prompt):
14
- """
15
- This function calls the loaded model with the user's prompt.
16
- gr.load(...) returns a Gradio interface object, so we can call it like a function.
17
- """
18
- # If the loaded model is a pipeline or interface, calling it directly returns the response.
19
- response = model(prompt)
20
- return response
21
-
22
  with gr.Blocks(css=css) as demo:
23
- # Greeting at the top
24
  gr.Markdown("<h1 style='text-align: center;'>Bonjour Dans le chat du consentement</h1>")
25
-
26
- # Create the input/output layout
27
- with gr.Row():
28
- user_input = gr.Textbox(label="Entrez votre message ici:", lines=3)
29
- output = gr.Textbox(label="Réponse du Modèle Mistral-7B-Instruct:", lines=5)
30
  send_button = gr.Button("Envoyer")
31
 
32
- # Link the button to inference_fn
33
  send_button.click(fn=inference_fn, inputs=user_input, outputs=output)
34
 
35
- # Launch the app
36
- if __name__ == "__main__":
37
- demo.launch()
 
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()