Tech-Meld commited on
Commit
d59cc6b
·
verified ·
1 Parent(s): 652eacf

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -0
app.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
+ from playwright.sync_api import sync_playwright
4
+ from flax import linen as nn
5
+ from jax import random
6
+ import jax
7
+ import jax.numpy as jnp
8
+
9
+ # Define LLaVA model parameters
10
+ MODEL_NAME = "HuggingFaceH4/zephyr-7b-beta"
11
+ MAX_LENGTH = 512
12
+ NUM_BEAMS = 5
13
+
14
+ # Define Flax model for action generation
15
+ class ActionModel(nn.Module):
16
+ vocab_size: int
17
+ hidden_size: int
18
+ num_layers: int
19
+
20
+ def setup(self):
21
+ self.embedding = nn.Embed(self.vocab_size, self.hidden_size)
22
+ self.lstm = nn.LSTM(self.hidden_size, self.hidden_size, num_layers=self.num_layers)
23
+ self.dense = nn.Dense(self.vocab_size)
24
+
25
+ def __call__(self, inputs, init_state):
26
+ embedded = self.embedding(inputs)
27
+ output, new_state = self.lstm(embedded, init_state)
28
+ logits = self.dense(output)
29
+ return logits, new_state
30
+
31
+ # Initialize Flax model
32
+ vocab_size = 50257
33
+ hidden_size = 1024
34
+ num_layers = 2
35
+ key = random.PRNGKey(0)
36
+ model = ActionModel(vocab_size, hidden_size, num_layers)
37
+ init_state = model.lstm.initialize_carry(key, (1, hidden_size))
38
+
39
+ # Function to generate actions using LLaVA model
40
+ def generate_actions(input_text, browser, page):
41
+ # Load LLaVA model
42
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
43
+ model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
44
+
45
+ # Prepare input for LLaVA
46
+ inputs = tokenizer(input_text, return_tensors="pt")
47
+ inputs = inputs.to(model.device)
48
+
49
+ # Generate response
50
+ outputs = model.generate(
51
+ input_ids=inputs.input_ids,
52
+ max_length=MAX_LENGTH,
53
+ num_beams=NUM_BEAMS,
54
+ temperature=0.7,
55
+ )
56
+
57
+ # Decode response and extract actions
58
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
59
+ actions = response.split("\n")
60
+
61
+ # Perform actions
62
+ for action in actions:
63
+ if "open website" in action:
64
+ website = action.split(" ")[-1]
65
+ page.goto(website)
66
+ elif "click" in action:
67
+ selector = action.split(" ")[-1]
68
+ page.click(selector)
69
+ elif "type" in action:
70
+ text = action.split(" ")[-1]
71
+ page.type(text)
72
+ elif "submit" in action:
73
+ page.press("Enter")
74
+ else:
75
+ print(f"Action not recognized: {action}")
76
+
77
+ # Function to initialize browser and page
78
+ def initialize_browser():
79
+ with sync_playwright() as p:
80
+ browser = p.chromium.launch()
81
+ page = browser.new_page()
82
+ return browser, page
83
+
84
+ # Gradio interface
85
+ def run_agent(input_text):
86
+ with sync_playwright() as p:
87
+ browser, page = initialize_browser()
88
+ generate_actions(input_text, browser, page)
89
+ return f"Successfully executed actions based on: {input_text}"
90
+
91
+ iface = gr.Interface(
92
+ fn=run_agent,
93
+ inputs=gr.Textbox(label="Enter your request"),
94
+ outputs=gr.Textbox(label="Response"),
95
+ title="Automated Agent",
96
+ description="Enter a task or instruction for the agent to perform."
97
+ )
98
+ iface.launch()