Sairesh commited on
Commit
94285a7
Β·
verified Β·
1 Parent(s): 47b0376

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -0
app.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import AutoProcessor, AutoModelForCausalLM
4
+ from PIL import Image
5
+
6
+ # --- STRATEGY: MEMORY MANAGEMENT ---
7
+ # We force the device to "cpu" because we are on the free tier.
8
+ # We trust remote code because Florence-2 uses custom architecture.
9
+
10
+ print(">>> INITIALIZING THE BRAIN...")
11
+
12
+ # 1. LOAD FLORENCE-2 (The Eyes)
13
+ # This model converts the UI screenshot into text/coordinates.
14
+ FLORENCE_ID = "microsoft/Florence-2-base"
15
+ print(f"Loading {FLORENCE_ID}...")
16
+ flo_model = AutoModelForCausalLM.from_pretrained(FLORENCE_ID, trust_remote_code=True).to("cpu").eval()
17
+ flo_processor = AutoProcessor.from_pretrained(FLORENCE_ID, trust_remote_code=True)
18
+
19
+ # 2. LOAD DOLPHIN-QWEN (The Logic)
20
+ # This model decides what to do based on what Florence sees.
21
+ DOLPHIN_ID = "cognitivecomputations/dolphin-2.9.4-qwen2-1.5b"
22
+ print(f"Loading {DOLPHIN_ID}...")
23
+ dolphin_model = AutoModelForCausalLM.from_pretrained(DOLPHIN_ID).to("cpu").eval()
24
+ dolphin_processor = AutoProcessor.from_pretrained(DOLPHIN_ID)
25
+
26
+ # --- THE LOGIC LOOP ---
27
+ def run_brain(image, user_instruction):
28
+ if image is None:
29
+ return "Error: No image provided."
30
+
31
+ # STEP A: Use Florence to find elements in the image
32
+ # We ask it to describe the UI or find specific widgets
33
+ prompt = "<OD>" # Object Detection prompt
34
+
35
+ inputs = flo_processor(text=prompt, images=image, return_tensors="pt").to("cpu")
36
+
37
+ with torch.no_grad():
38
+ generated_ids = flo_model.generate(
39
+ input_ids=inputs["input_ids"],
40
+ pixel_values=inputs["pixel_values"],
41
+ max_new_tokens=1024,
42
+ num_beams=3
43
+ )
44
+
45
+ # Decode Florence's vision into text
46
+ vision_text = flo_processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
47
+
48
+ # STEP B: Pass Vision Data to Dolphin (The Planning)
49
+ # We format the prompt so Dolphin knows what is on screen
50
+ dolphin_prompt = (
51
+ f"User Instruction: {user_instruction}\n"
52
+ f"Screen Analysis: {vision_text}\n"
53
+ f"Task: Decide which element to click. Return the HEX Packet ID."
54
+ )
55
+
56
+ # (Simple Dolphin inference for now - we will fine-tune this later)
57
+ dolphin_inputs = dolphin_processor(dolphin_prompt, return_tensors="pt").to("cpu")
58
+
59
+ with torch.no_grad():
60
+ output_ids = dolphin_model.generate(**dolphin_inputs, max_new_tokens=50)
61
+
62
+ final_decision = dolphin_processor.decode(output_ids[0], skip_special_tokens=True)
63
+
64
+ return f"Vision Saw: {vision_text}\n\nBrain Decided: {final_decision}"
65
+
66
+ # --- USER INTERFACE ---
67
+ demo = gr.Interface(
68
+ fn=run_brain,
69
+ inputs=[gr.Image(label="Android Screenshot", type="pil"), gr.Textbox(label="Goal (e.g., Open Game)")],
70
+ outputs="text",
71
+ title="Android Automation Brain",
72
+ description="Florence-2 for Vision + Dolphin for Logic"
73
+ )
74
+
75
+ if __name__ == "__main__":
76
+ demo.launch()