Princess3 commited on
Commit
660382d
1 Parent(s): 9a80baf

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -261
app.py DELETED
@@ -1,261 +0,0 @@
1
- import fastapi
2
- from typing import Optional, Dict, Any
3
- import copy
4
- import requests
5
- import json
6
- import os
7
- import sys
8
- from io import StringIO
9
- import ctypes
10
- import subprocess
11
- import logging
12
- from pathlib import Path
13
- from llama_cpp import Llama
14
- from concurrent.futures import ThreadPoolExecutor, as_completed
15
- import random
16
- import time
17
- import inspect
18
- # Load model directly
19
- from transformers import AutoTokenizer, AutoModelForCausalLM
20
-
21
- tokenizer = AutoTokenizer.from_pretrained("meetkai/functionary-small-v3.2-GGUF", trust_remote_code=True)
22
- model_path = AutoModelForCausalLM.from_pretrained("sentence-transformers/all-MiniLM-L6-v2", trust_remote_code=True)
23
- # Configure logging
24
- logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
25
- logger = logging.getLogger(__name__)
26
-
27
- class Tool(fastapi.FastAPI):
28
- def __init__(
29
- self,
30
- tool_name: str,
31
- description: str,
32
- name_for_human: Optional[str] = None,
33
- name_for_model: Optional[str] = None,
34
- description_for_human: Optional[str] = None,
35
- description_for_model: Optional[str] = None,
36
- logo_url: Optional[str] = None,
37
- author_github: Optional[str] = None,
38
- contact_email: str = "",
39
- legal_info_url: str = "",
40
- version: str = "0.1.0",
41
- ):
42
- super().__init__(
43
- title=tool_name,
44
- description=description,
45
- version=version,
46
- )
47
-
48
- if name_for_human is None:
49
- name_for_human = tool_name
50
- if name_for_model is None:
51
- name_for_model = name_for_human
52
- if description_for_human is None:
53
- description_for_human = description
54
- if description_for_model is None:
55
- description_for_model = description_for_human
56
-
57
- self.api_info = {
58
- "schema_version": "v1",
59
- "name_for_human": name_for_human,
60
- "name_for_model": name_for_model,
61
- "description_for_human": description_for_human,
62
- "description_for_model": description_for_model,
63
- "auth": {
64
- "type": "none",
65
- },
66
- "api": {
67
- "type": "openapi",
68
- "url": "/openapi.json",
69
- "is_user_authenticated": False,
70
- },
71
- "author_github": author_github,
72
- "logo_url": logo_url,
73
- "contact_email": contact_email,
74
- "legal_info_url": legal_info_url,
75
- }
76
-
77
- @self.get("/.well-known/ai-plugin.json", include_in_schema=False)
78
- def get_api_info(request: fastapi.Request):
79
- openapi_path = str(request.url).replace("/.well-known/ai-plugin.json", "/openapi.json")
80
- info = copy.deepcopy(self.api_info)
81
- info["api"]["url"] = str(openapi_path)
82
- return info
83
-
84
- class SelfLearningTool:
85
- def __init__(self):
86
- self.tools = {}
87
-
88
- def add_tool(self, name: str, func: callable):
89
- self.tools[name] = func
90
-
91
- def use_tool(self, name: str, *args, **kwargs):
92
- if name in self.tools:
93
- return self.tools[name](*args, **kwargs)
94
- else:
95
- return f"Tool '{name}' not found."
96
-
97
- def list_tools(self):
98
- return list(self.tools.keys())
99
-
100
- def remove_tool(self, name: str):
101
- if name in self.tools:
102
- del self.tools[name]
103
- return f"Tool '{name}' removed successfully."
104
- else:
105
- return f"Tool '{name}' not found."
106
-
107
- class PythonREPL:
108
- def __init__(self):
109
- self.globals = {}
110
- self.locals = {}
111
- self.output_buffer = StringIO()
112
- self.self_learning_tool = SelfLearningTool()
113
-
114
- def run(self, command: str) -> str:
115
- old_stdout = sys.stdout
116
- sys.stdout = self.output_buffer
117
- try:
118
- exec(command, self.globals, self.locals)
119
- output = self.output_buffer.getvalue()
120
- except Exception as e:
121
- output = f"Error: {repr(e)}"
122
- finally:
123
- sys.stdout = old_stdout
124
- self.output_buffer.truncate(0)
125
- self.output_buffer.seek(0)
126
- return output
127
-
128
- def add_tool(self, name: str, func: callable):
129
- self.self_learning_tool.add_tool(name, func)
130
-
131
- def use_tool(self, name: str, *args, **kwargs):
132
- return self.self_learning_tool.use_tool(name, *args, **kwargs)
133
-
134
- def list_tools(self):
135
- return self.self_learning_tool.list_tools()
136
-
137
- def remove_tool(self, name: str):
138
- return self.self_learning_tool.remove_tool(name)
139
-
140
- def self_reflect(self):
141
- reflection = "Self-reflection:\n"
142
- reflection += f"Number of defined variables: {len(self.locals)}\n"
143
- reflection += f"Number of available tools: {len(self.list_tools())}\n"
144
- reflection += "Available tools:\n"
145
- for tool in self.list_tools():
146
- reflection += f"- {tool}\n"
147
- return reflection
148
-
149
- def self_inspect(self):
150
- inspection = "Self-inspection:\n"
151
- for name, value in self.locals.items():
152
- inspection += f"{name}: {type(value)}\n"
153
- if callable(value):
154
- try:
155
- signature = inspect.signature(value)
156
- inspection += f" Signature: {signature}\n"
157
- except ValueError:
158
- inspection += " Signature: Unable to inspect\n"
159
- return inspection
160
-
161
- def initialize_llm(model_path: str, n_ctx: int, n_threads: int = 4, n_batch: int = 512) -> Llama:
162
- try:
163
- return Llama(model_path=model_path, n_ctx=n_ctx, n_threads=n_threads, n_batch=n_batch, verbose=True)
164
- except Exception as e:
165
- logger.error(f"Failed to initialize LLM: {e}")
166
- raise
167
-
168
- llm = initialize_llm(model_path, 4096)
169
-
170
- def build_tool(config) -> Tool:
171
- tool = Tool(
172
- "Advanced Python REPL",
173
- "Execute sophisticated Python commands with self-learning capabilities",
174
- name_for_model="Advanced Python REPL",
175
- description_for_model=(
176
- "An advanced Python shell for executing complex Python commands. "
177
- "Input should be a valid Python command or script. "
178
- "Use print(...) to see the output of expressions. "
179
- "Capable of handling multi-line code, advanced Python features, "
180
- "and self-learning tools."
181
- ),
182
- logo_url="https://your-app-url.com/.well-known/logo.png",
183
- contact_email="hello@contact.com",
184
- legal_info_url="hello@legal.com"
185
- )
186
-
187
- python_repl = PythonREPL()
188
-
189
- def sanitize_input(query: str) -> str:
190
- return query.strip().strip("```").strip()
191
-
192
- @tool.get("/run_python")
193
- def run_python(query: str):
194
- sanitized_query = sanitize_input(query)
195
- result = python_repl.run(sanitized_query)
196
- return {"result": result, "execution_time": time.time()}
197
-
198
- @tool.get("/add_tool")
199
- def add_tool(name: str, code: str):
200
- sanitized_code = sanitize_input(code)
201
- try:
202
- exec(f"def {name}({sanitized_code})", python_repl.globals, python_repl.locals)
203
- python_repl.add_tool(name, python_repl.locals[name])
204
- return f"Tool '{name}' added successfully."
205
- except Exception as e:
206
- return f"Error adding tool: {str(e)}"
207
-
208
- @tool.get("/use_tool")
209
- def use_tool(name: str, args: str):
210
- try:
211
- result = python_repl.use_tool(name, *eval(args))
212
- return {"result": result}
213
- except Exception as e:
214
- return {"error": str(e)}
215
-
216
- @tool.get("/list_tools")
217
- def list_tools():
218
- return {"tools": python_repl.list_tools()}
219
-
220
- @tool.get("/remove_tool")
221
- def remove_tool(name: str):
222
- return {"result": python_repl.remove_tool(name)}
223
-
224
- @tool.get("/self_reflect")
225
- def self_reflect():
226
- return {"reflection": python_repl.self_reflect()}
227
-
228
- @tool.get("/self_inspect")
229
- def self_inspect():
230
- return {"inspection": python_repl.self_inspect()}
231
-
232
- @tool.get("/write_file")
233
- def write_file(file_path: str, text: str) -> str:
234
- write_path = Path(file_path)
235
- try:
236
- write_path.parent.mkdir(exist_ok=True, parents=False)
237
- with write_path.open("w", encoding="utf-8") as f:
238
- f.write(text)
239
- return f"File written successfully to {file_path}."
240
- except Exception as e:
241
- return "Error: " + str(e)
242
-
243
- @tool.get("/read_file")
244
- def read_file(file_path: str) -> str:
245
- read_path = Path(file_path)
246
- try:
247
- with read_path.open("r", encoding="utf-8") as f:
248
- content = f.read()
249
- return content
250
- except Exception as e:
251
- return "Error: " + str(e)
252
-
253
- return tool
254
-
255
- if __name__ == "__main__":
256
- config = {} # Add any necessary configuration
257
- advanced_python_repl = build_tool(config)
258
-
259
- # Run the FastAPI server
260
- import uvicorn
261
- uvicorn.run(advanced_python_repl, host="0.0.0.0", port=8000)