Create Importing_AI
Browse files- Importing_AI +46 -0
Importing_AI
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoProcessor, AutoModelForCausalLM, tqdm
|
| 4 |
+
|
| 5 |
+
# 1. Configuration & Model IDs
|
| 6 |
+
FLORENCE_MODEL_ID = "microsoft/Florence-2-base"
|
| 7 |
+
DOLPHIN_MODEL_ID = "cognitivecomputations/dolphin-2.9.4-qwen2-1.5b" # Or your merged model path
|
| 8 |
+
|
| 9 |
+
# 2. Load Florence-2 (Vision)
|
| 10 |
+
print("Loading Florence-2...")
|
| 11 |
+
florence_model = AutoModelForCausalLM.from_pretrained(FLORENCE_MODEL_ID, trust_remote_code=True).to("cpu").eval()
|
| 12 |
+
florence_processor = AutoProcessor.from_pretrained(FLORENCE_MODEL_ID, trust_remote_code=True)
|
| 13 |
+
|
| 14 |
+
# 3. Load Dolphin-Qwen (Reasoning)
|
| 15 |
+
print("Loading Dolphin-Qwen...")
|
| 16 |
+
dolphin_model = AutoModelForCausalLM.from_pretrained(DOLPHIN_MODEL_ID).to("cpu").eval()
|
| 17 |
+
dolphin_tokenizer = AutoProcessor.from_pretrained(DOLPHIN_MODEL_ID)
|
| 18 |
+
|
| 19 |
+
def process_ui_task(image, prompt):
|
| 20 |
+
# This function will eventually handle the phased logic:
|
| 21 |
+
# Phase 1: Florence identifies the button
|
| 22 |
+
# Phase 3: Dolphin maps it to a Hex Packet
|
| 23 |
+
|
| 24 |
+
# Simple Florence Inference Example
|
| 25 |
+
inputs = florence_processor(text="<OD>", images=image, return_tensors="pt").to("cpu")
|
| 26 |
+
generated_ids = florence_model.generate(
|
| 27 |
+
input_ids=inputs["input_ids"],
|
| 28 |
+
pixel_values=inputs["pixel_values"],
|
| 29 |
+
max_new_tokens=1024,
|
| 30 |
+
do_sample=False,
|
| 31 |
+
num_beams=3
|
| 32 |
+
)
|
| 33 |
+
results = florence_processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
|
| 34 |
+
|
| 35 |
+
return f"Brain Output: {results}"
|
| 36 |
+
|
| 37 |
+
# 4. Gradio Interface
|
| 38 |
+
interface = gr.Interface(
|
| 39 |
+
fn=process_ui_task,
|
| 40 |
+
inputs=[gr.Image(type="pill"), gr.Textbox(label="Instruction")],
|
| 41 |
+
outputs="text",
|
| 42 |
+
title="AI Automation Brain (Florence + Dolphin)"
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
if __name__ == "__main__":
|
| 46 |
+
interface.launch()
|