Faustrix commited on
Commit
1b4c032
·
1 Parent(s): 681e744

chore: Update app.py with improved Gradio interface for Sudoku Solver

Browse files
Files changed (2) hide show
  1. app.py +11 -5
  2. requirements.txt +5 -2
app.py CHANGED
@@ -1,15 +1,19 @@
1
  import gradio as gr
 
 
2
  from transformers import AutoModel, AutoTokenizer
3
 
 
 
4
  # Load the model
5
- model = AutoModel.from_pretrained("openbmb/MiniCPM-Llama3-V-2_5", trust_remote_code=True)
 
6
 
7
  # Load the tokenizer
8
  tokenizer = AutoTokenizer.from_pretrained("openbmb/MiniCPM-Llama3-V-2_5", trust_remote_code=True)
9
 
10
  model.eval()
11
 
12
-
13
  # Define the Gradio components
14
  image = gr.Image(type="pil", label="Image")
15
  question = gr.Textbox(value="Using the standard 9x9 sudoku format, solve the sudoku puzzle in the image correctly.", label="Question")
@@ -20,17 +24,19 @@ description = "Sudoku Solver using MiniCPM-Llama3-V-2_5"
20
 
21
  # Define the function for solving Sudoku
22
  def solve_sudoku(image, question):
 
 
23
  msgs = [{"role": "user", "content": question}]
24
  res = model.chat(
25
  image=image,
26
  msgs=msgs,
27
  tokenizer=tokenizer,
28
- sampling=True,
29
  temperature=0.7,
30
- stream=True,
31
  system_prompt="You are an expert in solving sudoku puzzles. Please solve the sudoku puzzle in the image correctly.",
32
  )
33
- return res
34
 
35
  # Create the Gradio interface
36
  demo = gr.Interface(
 
1
  import gradio as gr
2
+ import torch
3
+ from PIL import Image
4
  from transformers import AutoModel, AutoTokenizer
5
 
6
+ device = "cuda" if torch.cuda.is_available() else "cpu"
7
+
8
  # Load the model
9
+ model = AutoModel.from_pretrained("openbmb/MiniCPM-Llama3-V-2_5", trust_remote_code=True, torch_dtype=torch.float16)
10
+ model = model.to(device=device)
11
 
12
  # Load the tokenizer
13
  tokenizer = AutoTokenizer.from_pretrained("openbmb/MiniCPM-Llama3-V-2_5", trust_remote_code=True)
14
 
15
  model.eval()
16
 
 
17
  # Define the Gradio components
18
  image = gr.Image(type="pil", label="Image")
19
  question = gr.Textbox(value="Using the standard 9x9 sudoku format, solve the sudoku puzzle in the image correctly.", label="Question")
 
24
 
25
  # Define the function for solving Sudoku
26
  def solve_sudoku(image, question):
27
+ # Convert image to RGB format if not already in RGB
28
+ image = image.convert("RGB") if image.mode != 'RGB' else image
29
  msgs = [{"role": "user", "content": question}]
30
  res = model.chat(
31
  image=image,
32
  msgs=msgs,
33
  tokenizer=tokenizer,
34
+ sampling=False,
35
  temperature=0.7,
36
+ stream=False, # Enable streaming
37
  system_prompt="You are an expert in solving sudoku puzzles. Please solve the sudoku puzzle in the image correctly.",
38
  )
39
+ return "".join(res)
40
 
41
  # Create the Gradio interface
42
  demo = gr.Interface(
requirements.txt CHANGED
@@ -1,2 +1,5 @@
1
- transformers
2
- torch
 
 
 
 
1
+ Pillow==10.1.0
2
+ torch==2.1.2
3
+ torchvision==0.16.2
4
+ transformers==4.40.0
5
+ sentencepiece==0.1.99