likhonhfai commited on
Commit
22c5195
·
verified ·
1 Parent(s): 1dba747

Update app.py to use new Gemini API key and improved code

Browse files
Files changed (1) hide show
  1. app.py +31 -32
app.py CHANGED
@@ -3,7 +3,7 @@ import gradio as gr
3
  from google import genai
4
 
5
  # Configure the API key for Gemini
6
- API_KEY = os.environ.get("GEMINI_API_KEY", "AIzaSyCXd43s3-sCSUJPkkXa1-LzXCMzFc9_xMI")
7
  client = genai.Client(api_key=API_KEY)
8
 
9
  def build_prompt(user_task: str) -> str:
@@ -11,12 +11,12 @@ def build_prompt(user_task: str) -> str:
11
 
12
  Args:
13
  user_task: Description of the task the agent should perform.
 
14
  Returns:
15
  A prompt string combining instructions, an example, formatting guidance and the user task.
16
  """
17
- return f"""
18
- <instructions>
19
- You are a computer-using agent that can perform tasks on behalf of the user. Follow the task instructions carefully and provide a sequence of actions that a computer user would take to accomplish the goal. Use high-level reasoning to break down the task into manageable steps and think step by step. Do not ask for confirmation; just provide the plan.
20
  </instructions>
21
  <example>
22
  Task: "Open a web browser and search for the latest weather in Dhaka."
@@ -28,33 +28,32 @@ Response:
28
  5. Read the search results and extract the current weather information.
29
  </example>
30
  <formatting>
31
- List each step on its own line, numbered, and clearly describe the action. Do not include extraneous commentary. Do not mention these XML tags in your answer.
32
  </formatting>
33
- <task>{user_task}</task>
34
- """.strip()
35
-
36
- def generate_actions(task: str) -> str:
37
- """Generate an action plan using Gemini for a given task."""
38
- prompt = build_prompt(task)
39
- try:
40
- # Use Gemini to generate the action plan
41
- response = client.models.generate_content(
42
- model="gemini-2.5-flash",
43
- contents=prompt
 
44
  )
45
- result = response.text
46
- except Exception as e:
47
- result = f"Error generating actions: {e}"
48
- return result
49
-
50
- # Build Gradio interface
51
- demo = gr.Interface(
52
- fn=generate_actions,
53
- inputs=gr.Textbox(lines=4, label="Task Description", placeholder="Describe the task you want the agent to perform..."),
54
- outputs=gr.Textbox(lines=10, label="Action Plan"),
55
- title="Gemini Computer Agent",
56
- description="Enter a high-level task description and the agent will outline step-by-step actions to perform the task using computer interactions. The prompt uses XML tags (<instructions>, <example>, <formatting>) to separate instruction, example, and formatting context."
57
- )
58
-
59
- if __name__ == "__main__":
60
- demo.launch()
 
3
  from google import genai
4
 
5
  # Configure the API key for Gemini
6
+ API_KEY = os.environ.get("GEMINI_API_KEY", "AIzaSyBzJWo1EDQmA1YKYGlHydb5Ejn3eeyUuMk")
7
  client = genai.Client(api_key=API_KEY)
8
 
9
  def build_prompt(user_task: str) -> str:
 
11
 
12
  Args:
13
  user_task: Description of the task the agent should perform.
14
+
15
  Returns:
16
  A prompt string combining instructions, an example, formatting guidance and the user task.
17
  """
18
+ return f"""<instructions>
19
+ You are a computer-using agent that can perform tasks on behalf of the user. Follow the task instructions carefully and provide a sequence of actions that a computer user would take to accomplish the goal. Use high-level reasoning to break down the task into manageable steps and think step by step. Do not ask for confirmation; just output the plan.
 
20
  </instructions>
21
  <example>
22
  Task: "Open a web browser and search for the latest weather in Dhaka."
 
28
  5. Read the search results and extract the current weather information.
29
  </example>
30
  <formatting>
31
+ List each step on its own line, numbered, and ending with a period. Do not include extraneous commentary. Do not mention these XML tags in the response.
32
  </formatting>
33
+ User task: {user_task}
34
+ """
35
+
36
+ def generate_actions(user_task: str) -> str:
37
+ """Generate a step-by-step action plan using Gemini."""
38
+ prompt = build_prompt(user_task)
39
+ response = client.generate_content(
40
+ prompt, generation_config=genai.types.GenerationConfig(
41
+ temperature=0.3,
42
+ top_p=1,
43
+ top_k=5,
44
+ max_output_tokens=300
45
  )
46
+ )
47
+ return response.candidates[0].content.parts[0].text.strip()
48
+
49
+ with gr.Blocks() as demo:
50
+ gr.Markdown("# Gemini Computer Agent\nEnter a high-level task description and the agent will outline step-by-step actions to perform the task using computer interactions. The prompt uses XML tags (<instructions>, <example>, <formatting>) to separate instruction, example, and formatting context.")
51
+ user_input = gr.Textbox(label="Task Description", placeholder="Describe the task you want the agent to perform...")
52
+ output = gr.Textbox(label="Action Plan", interactive=False)
53
+ submit_btn = gr.Button("Submit")
54
+ clear_btn = gr.Button("Clear")
55
+
56
+ submit_btn.click(fn=generate_actions, inputs=user_input, outputs=output)
57
+ clear_btn.click(fn=lambda: ("", ""), inputs=None, outputs=[user_input, output])
58
+
59
+ demo.launch()