Spaces:
Sleeping
Sleeping
Update app.py to use new Gemini API key and improved code
Browse files
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", "
|
| 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 |
-
|
| 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
|
| 32 |
</formatting>
|
| 33 |
-
|
| 34 |
-
"""
|
| 35 |
-
|
| 36 |
-
def generate_actions(
|
| 37 |
-
"""Generate
|
| 38 |
-
prompt = build_prompt(
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
| 44 |
)
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 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()
|
|
|
|
|
|