srivatsavdamaraju commited on
Commit
fa725d5
1 Parent(s): 25b37ff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -28
app.py CHANGED
@@ -1,42 +1,43 @@
1
  import gradio as gr
2
- from transformers import AutoModel, AutoTokenizer
3
- from PIL import Image
4
  import torch
 
 
5
 
6
- # Load the model and tokenizer
7
- model_path = "./ggml-model-IQ3_M.gguf" # Adjust the path to your .gguf model
8
- model = AutoModel.from_pretrained(model_path, torch_dtype=torch.bfloat16)
9
- tokenizer = AutoTokenizer.from_pretrained(model_path)
10
 
11
- # Function to generate responses
12
- def generate_response(question, image=None):
13
- # Tokenize the question input
14
- inputs = tokenizer(question, return_tensors="pt")
15
 
16
- # Process the image if provided
17
- if image is not None:
18
- image = Image.open(image).convert("RGB")
19
- # You may need to preprocess the image here (e.g., resizing)
20
 
21
- # Generate output from the model
22
- with torch.no_grad():
23
- output = model(**inputs) # Adjust based on your model's input requirements
24
 
25
- # Extract the response
26
- response = output # Modify this to match your model's output structure
27
- return response # Convert the response to a string if necessary
 
 
 
 
 
 
28
 
29
  # Create the Gradio interface
30
- interface = gr.Interface(
31
- fn=generate_response,
32
  inputs=[
33
- gr.Textbox(label="Enter your question"),
34
- gr.Image(label="Upload an image", type="filepath")
35
  ],
36
  outputs="text",
37
- title="MiniCPM Q&A Model",
38
- description="Ask questions about the uploaded image."
39
  )
40
 
41
- # Launch the interface
42
- interface.launch()
 
1
  import gradio as gr
 
 
2
  import torch
3
+ from PIL import Image
4
+ from transformers import AutoModel, AutoTokenizer
5
 
6
+ # Load the model and tokenizer from the local path
7
+ model = AutoModel.from_pretrained('minicpm/models', trust_remote_code=True)
8
+ tokenizer = AutoTokenizer.from_pretrained('minicpm/models', trust_remote_code=True)
 
9
 
10
+ # Set the model to evaluation mode
11
+ model.eval()
 
 
12
 
13
+ def predict(image, question):
14
+ # Preprocess the image
15
+ image = image.convert('RGB')
 
16
 
17
+ # Create the message list
18
+ msgs = [{'role': 'user', 'content': question}]
 
19
 
20
+ # Generate a response
21
+ res = model.chat(
22
+ image=image,
23
+ msgs=msgs,
24
+ tokenizer=tokenizer,
25
+ sampling=True,
26
+ temperature=0.1
27
+ )
28
+ return res
29
 
30
  # Create the Gradio interface
31
+ iface = gr.Interface(
32
+ fn=predict,
33
  inputs=[
34
+ gr.inputs.Image(type="pil", label="Upload an Image"),
35
+ gr.inputs.Textbox(label="Ask a Question")
36
  ],
37
  outputs="text",
38
+ title="Image Question Answering",
39
+ description="Upload an image and ask a question about it."
40
  )
41
 
42
+ # Launch the app
43
+ iface.launch()