hossamelden commited on
Commit
5b0c4ad
1 Parent(s): a3d995a

Upload trymistral1.py

Browse files
Files changed (1) hide show
  1. trymistral1.py +102 -0
trymistral1.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """trymistral1.ipynb
3
+
4
+ Automatically generated by Colab.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1tBaMrUjepPv483Xhmfh9QbZHtB-SC1wG
8
+ """
9
+
10
+ # to get packages initially installed in colab or in other word, the colab environment
11
+ !pip3 freeze > requirements.txt
12
+
13
+ # Step 1: Install the necessary libraries
14
+ !pip install transformers
15
+ !pip install datasets
16
+ !pip install huggingface_hub
17
+ !pip install accelerate bitsandbytes mistral_inference # Make sure to install mistral_inference
18
+
19
+ # Step 2: Authenticate with Hugging Face
20
+ from huggingface_hub import login
21
+
22
+ # Replace 'your_hf_token' with your actual token
23
+ login(token="")
24
+
25
+ !pip install torch
26
+ import torch
27
+ !pip install mistral_inference
28
+ # Install necessary libraries
29
+ !pip install transformers gradio bitsandbytes
30
+
31
+ !pip show mistral_inference
32
+
33
+ !pip list
34
+
35
+ # Step 3: Load the model and tokenizer
36
+ from transformers import AutoTokenizer, MistralForCausalLM
37
+ from transformers import AutoModelForCausalLM, BitsAndBytesConfig
38
+
39
+
40
+
41
+ import gradio as gr
42
+ from PIL import Image
43
+ import io
44
+ import base64
45
+
46
+ def image_to_base64(image):
47
+ buffered = io.BytesIO()
48
+ image.save(buffered, format="png")
49
+ img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
50
+ return img_str
51
+
52
+ def chat_with_llava(image, question):
53
+ try:
54
+ # Convert image to base64 (if needed, but LLAVA model might not use images)
55
+ image_b64 = image_to_base64(image)
56
+
57
+ # Prepare input
58
+ inputs = tokenizer(question, return_tensors="pt")
59
+
60
+ # Generate text
61
+ outputs = model.generate(**inputs)
62
+ generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
63
+
64
+ return generated_text
65
+
66
+ except Exception as e:
67
+ return f"Error occurred: {str(e)}"
68
+
69
+ model_id = "microsoft/llava-med-v1.5-mistral-7b"
70
+
71
+ bnb_config = BitsAndBytesConfig(
72
+ load_in_4bit=True,
73
+ bnb_4bit_use_double_quant=True,
74
+ bnb_4bit_quant_type="nf4",
75
+ bnb_4bit_compute_dtype=torch.bfloat16
76
+ )
77
+
78
+ # Load the tokenizer
79
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
80
+
81
+ # Load the model using MistralForCausalLM
82
+ # Use the appropriate model class for 'llava_mistral' architecture
83
+ model = MistralForCausalLM.from_pretrained(model_id, quantization_config=bnb_config, device_map="auto")
84
+
85
+
86
+
87
+ # Create a Gradio interface
88
+ iface = gr.Interface(
89
+ fn=chat_with_llava,
90
+ inputs=[gr.Image(type="pil", label="Upload Image"), gr.Textbox(lines=1, label="Ask a question")],
91
+ outputs=gr.Textbox(label="Response"),
92
+ title="LLAVA Model Chat with Image",
93
+ description="Upload an image and ask a question to the LLAVA model.",
94
+ )
95
+
96
+ # Launch the Gradio interface
97
+ iface.launch()
98
+
99
+
100
+
101
+
102
+