File size: 4,711 Bytes
bcc0784
 
 
 
 
b891612
bcc0784
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
---
title: Jarvis Mobile Agent
emoji: πŸ€–
colorFrom: blue
colorTo: purple
sdk: docker
pinned: false
---

# πŸ€– Jarvis β€” AI Automation Planner for Mobile Devices

Jarvis is an intelligent AI automation planner that converts user voice/text commands into structured JSON task plans for mobile device automation.

## Architecture

```
Mobile Application
      ↓
HTTP POST β†’ /v1/chat/completions
      ↓
Jarvis AI (Reason β†’ Plan β†’ Output)
      ↓
JSON automation plan returned
      ↓
Mobile automation engine executes locally
```

## Project Structure

```
AI-Agent/
β”œβ”€β”€ app.py                  # Main app β€” Gradio UI + FastAPI endpoints
β”œβ”€β”€ jarvis/
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ agent.py            # Core agent (Reason β†’ Plan β†’ Execute)
β”‚   β”œβ”€β”€ tools.py            # Dynamic tool registry
β”‚   β”œβ”€β”€ security.py         # Restricted-action enforcement
β”‚   └── prompts.py          # System prompt builder
β”œβ”€β”€ tool_registry.json      # Tool definitions (editable without code changes)
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ Dockerfile
└── README.md
```

## API Endpoint

### `POST /v1/chat/completions`

OpenAI-compatible chat completions endpoint.

**Request:**
```json
{
  "messages": [
    { "role": "user", "content": "Download a galaxy image and send it to Arun" }
  ]
}
```

**Response:**
```json
{
  "id": "chatcmpl-abc123",
  "object": "chat.completion",
  "model": "Qwen/Qwen2-1.5B-Instruct",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "{\"steps\":[{\"tool\":\"download_image\",\"query\":\"galaxy\"},{\"tool\":\"send_message\",\"contact\":\"Arun\",\"message\":\"Sending the galaxy image\"}]}"
      },
      "finish_reason": "stop"
    }
  ]
}
```

### `GET /health`

Returns service status and available tools.

### `GET /v1/models`

Lists the currently loaded model.

## Supported Tools

| Tool | Description | Parameters |
|------|-------------|------------|
| `open_app` | Open a device application | `app_name` |
| `search_google` | Internet search | `query` |
| `download_image` | Download an image | `query` |
| `download_video` | Download a video | `url` |
| `send_message` | Send a message to a contact | `contact`, `message` |
| `product_search` | Search e-commerce platforms | `query` |
| `set_alarm` | Create alarm/reminder | `time` |
| `save_file` | Download and store a file | `url` |

New tools can be added by editing `tool_registry.json` β€” no code changes needed.

## Security Restrictions

Jarvis refuses to automate:
- Banking / payment apps (GPay, PhonePe, Paytm, etc.)
- Financial transactions
- Password managers
- Personal identity data (Aadhaar, PAN, SSN, etc.)

Restricted requests return:
```json
{ "error": "This action is restricted for security reasons." }
```

## Deployment to Hugging Face Spaces

### Option A β€” Gradio SDK (recommended)

1. Create a new Space at [huggingface.co/new-space](https://huggingface.co/new-space)
2. Choose **SDK: Gradio**
3. Push this repository to the Space
4. Add your `HF_TOKEN` as a Space secret (Settings β†’ Secrets)
5. The Space will be available at:
   ```
   POST https://<your-space>.hf.space/v1/chat/completions
   ```

### Option B β€” Docker SDK

1. Create a new Space with **SDK: Docker**
2. Push this repository (the `Dockerfile` is included)
3. Add `HF_TOKEN` as a Space secret

## Environment Variables

| Variable | Default | Description |
|----------|---------|-------------|
| `JARVIS_MODEL` | `Qwen/Qwen2-1.5B-Instruct` | HF model ID for the planner LLM |
| `HF_TOKEN` | *(none)* | Hugging Face API token for gated models |
| `PORT` | `7860` | Server port |

## Local Development

```bash
pip install -r requirements.txt
export HF_TOKEN="hf_..."
python app.py
```

The Gradio UI will be available at `http://localhost:7860` and the API at `http://localhost:7860/v1/chat/completions`.

## Mobile Integration (Android Example)

```kotlin
val client = OkHttpClient()
val json = """{"messages":[{"role":"user","content":"$userCommand"}]}"""
val body = json.toRequestBody("application/json".toMediaType())

val request = Request.Builder()
    .url("https://Valtry-mobile-agent.hf.space/v1/chat/completions")
    .post(body)
    .build()

client.newCall(request).enqueue(object : Callback {
    override fun onResponse(call: Call, response: Response) {
        val plan = JSONObject(response.body!!.string())
            .getJSONArray("choices")
            .getJSONObject(0)
            .getJSONObject("message")
            .getString("content")
        // Parse and execute the automation plan
    }
    override fun onFailure(call: Call, e: IOException) { /* handle */ }
})
```