Spaces:
Sleeping
Sleeping
TuringsSolutions
commited on
Commit
•
54e35ae
1
Parent(s):
c552f2d
Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,7 @@ import json
|
|
4 |
import numpy as np
|
5 |
import requests
|
6 |
from openai import OpenAI
|
|
|
7 |
|
8 |
def call_gpt3_5(prompt, api_key):
|
9 |
client = OpenAI(api_key=api_key)
|
@@ -11,7 +12,7 @@ def call_gpt3_5(prompt, api_key):
|
|
11 |
response = client.chat.completions.create(
|
12 |
model="gpt-3.5-turbo",
|
13 |
messages=[
|
14 |
-
{"role": "system", "content": "You are a
|
15 |
{"role": "user", "content": prompt}
|
16 |
]
|
17 |
)
|
@@ -21,26 +22,54 @@ def call_gpt3_5(prompt, api_key):
|
|
21 |
|
22 |
def execute_snn(api_url, openai_api_key, num_agents, calls_per_agent, special_config):
|
23 |
prompt = f"""
|
24 |
-
Construct
|
25 |
- API URL: {api_url}
|
26 |
- Number of Agents: {num_agents}
|
27 |
- Calls per Agent: {calls_per_agent}
|
28 |
- Special Configuration: {special_config if special_config else 'None'}
|
29 |
|
30 |
-
|
31 |
-
1.
|
32 |
-
2.
|
33 |
-
3.
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
-
|
36 |
"""
|
37 |
|
38 |
-
|
39 |
|
40 |
-
if
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
else:
|
43 |
-
return
|
44 |
|
45 |
# Define the Gradio interface
|
46 |
iface = gr.Interface(
|
@@ -54,7 +83,7 @@ iface = gr.Interface(
|
|
54 |
],
|
55 |
outputs="text",
|
56 |
title="Swarm Neural Network Simulator",
|
57 |
-
description="Enter the parameters for your Swarm Neural Network (SNN) simulation.",
|
58 |
examples=[
|
59 |
["https://meowfacts.herokuapp.com/", "your-api-key-here", 3, 1, ""],
|
60 |
["https://api.publicapis.org/entries", "your-api-key-here", 5, 2, "category=Animals"]
|
|
|
4 |
import numpy as np
|
5 |
import requests
|
6 |
from openai import OpenAI
|
7 |
+
import ast
|
8 |
|
9 |
def call_gpt3_5(prompt, api_key):
|
10 |
client = OpenAI(api_key=api_key)
|
|
|
12 |
response = client.chat.completions.create(
|
13 |
model="gpt-3.5-turbo",
|
14 |
messages=[
|
15 |
+
{"role": "system", "content": "You are a Python expert capable of constructing and executing a Swarm Neural Network (SNN). Return only the Python code for the SNN."},
|
16 |
{"role": "user", "content": prompt}
|
17 |
]
|
18 |
)
|
|
|
22 |
|
23 |
def execute_snn(api_url, openai_api_key, num_agents, calls_per_agent, special_config):
|
24 |
prompt = f"""
|
25 |
+
Construct a Swarm Neural Network (SNN) in Python with the following parameters:
|
26 |
- API URL: {api_url}
|
27 |
- Number of Agents: {num_agents}
|
28 |
- Calls per Agent: {calls_per_agent}
|
29 |
- Special Configuration: {special_config if special_config else 'None'}
|
30 |
|
31 |
+
The SNN should:
|
32 |
+
1. Initialize the specified number of agents
|
33 |
+
2. Have each agent make the specified number of API calls
|
34 |
+
3. Process the data retrieved from the API calls
|
35 |
+
4. Implement a simple collective behavior mechanism
|
36 |
+
5. Return a dictionary with the following keys:
|
37 |
+
- 'data_summary': A summary of the data retrieved
|
38 |
+
- 'insights': Any patterns or insights derived from the collective behavior
|
39 |
+
- 'performance': Performance metrics (e.g., execution time, success rate of API calls)
|
40 |
|
41 |
+
Provide only the Python code to implement this SNN. The code should be fully functional and ready to execute.
|
42 |
"""
|
43 |
|
44 |
+
snn_code = call_gpt3_5(prompt, openai_api_key)
|
45 |
|
46 |
+
if not snn_code.startswith("Error"):
|
47 |
+
try:
|
48 |
+
# Add necessary imports to the generated code
|
49 |
+
full_code = f"""
|
50 |
+
import requests
|
51 |
+
import time
|
52 |
+
import numpy as np
|
53 |
+
|
54 |
+
{snn_code}
|
55 |
+
|
56 |
+
# Execute the SNN
|
57 |
+
snn = SwarmNeuralNetwork("{api_url}", {num_agents}, {calls_per_agent})
|
58 |
+
result = snn.execute()
|
59 |
+
print(result)
|
60 |
+
"""
|
61 |
+
|
62 |
+
# Execute the generated code
|
63 |
+
exec_globals = {}
|
64 |
+
exec(full_code, exec_globals)
|
65 |
+
|
66 |
+
# Retrieve the result from the executed code
|
67 |
+
result = exec_globals.get('result', "No result returned from SNN execution.")
|
68 |
+
return f"Results from the swarm neural network:\n\n{json.dumps(result, indent=2)}"
|
69 |
+
except Exception as e:
|
70 |
+
return f"Error executing SNN code: {str(e)}\n\nGenerated code:\n{snn_code}"
|
71 |
else:
|
72 |
+
return snn_code
|
73 |
|
74 |
# Define the Gradio interface
|
75 |
iface = gr.Interface(
|
|
|
83 |
],
|
84 |
outputs="text",
|
85 |
title="Swarm Neural Network Simulator",
|
86 |
+
description="Enter the parameters for your Swarm Neural Network (SNN) simulation. The SNN will be constructed and executed based on your inputs.",
|
87 |
examples=[
|
88 |
["https://meowfacts.herokuapp.com/", "your-api-key-here", 3, 1, ""],
|
89 |
["https://api.publicapis.org/entries", "your-api-key-here", 5, 2, "category=Animals"]
|