Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,7 @@ import json
|
|
| 4 |
import numpy as np
|
| 5 |
import requests
|
| 6 |
from openai import OpenAI
|
| 7 |
-
import
|
| 8 |
|
| 9 |
def call_gpt3_5(prompt, api_key):
|
| 10 |
client = OpenAI(api_key=api_key)
|
|
@@ -12,7 +12,7 @@ def call_gpt3_5(prompt, 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
|
| 16 |
{"role": "user", "content": prompt}
|
| 17 |
]
|
| 18 |
)
|
|
@@ -20,58 +20,82 @@ def call_gpt3_5(prompt, api_key):
|
|
| 20 |
except Exception as e:
|
| 21 |
return f"Error calling GPT-3.5: {str(e)}"
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
def execute_snn(api_url, openai_api_key, num_agents, calls_per_agent, special_config):
|
| 24 |
prompt = f"""
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 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 |
-
|
| 45 |
|
| 46 |
-
if not
|
| 47 |
try:
|
| 48 |
-
#
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
snn = SwarmNeuralNetwork("{api_url}", {num_agents}, {calls_per_agent})
|
| 58 |
-
result = snn.execute()
|
| 59 |
-
print(result)
|
| 60 |
-
"""
|
| 61 |
|
| 62 |
-
#
|
| 63 |
-
|
| 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
|
| 71 |
else:
|
| 72 |
-
return
|
| 73 |
|
| 74 |
-
# Define the Gradio interface
|
| 75 |
iface = gr.Interface(
|
| 76 |
fn=execute_snn,
|
| 77 |
inputs=[
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
import requests
|
| 6 |
from openai import OpenAI
|
| 7 |
+
import time
|
| 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 implementing specific functions for a Swarm Neural Network (SNN). Return only the Python code for the requested function."},
|
| 16 |
{"role": "user", "content": prompt}
|
| 17 |
]
|
| 18 |
)
|
|
|
|
| 20 |
except Exception as e:
|
| 21 |
return f"Error calling GPT-3.5: {str(e)}"
|
| 22 |
|
| 23 |
+
class Agent:
|
| 24 |
+
def __init__(self, api_url):
|
| 25 |
+
self.api_url = api_url
|
| 26 |
+
self.data = None
|
| 27 |
+
|
| 28 |
+
def make_api_call(self):
|
| 29 |
+
try:
|
| 30 |
+
response = requests.get(self.api_url)
|
| 31 |
+
if response.status_code == 200:
|
| 32 |
+
self.data = response.json()
|
| 33 |
+
else:
|
| 34 |
+
self.data = {"error": f"API call failed with status code {response.status_code}"}
|
| 35 |
+
except Exception as e:
|
| 36 |
+
self.data = {"error": str(e)}
|
| 37 |
+
|
| 38 |
+
class SwarmNeuralNetwork:
|
| 39 |
+
def __init__(self, api_url, num_agents, calls_per_agent, special_config):
|
| 40 |
+
self.api_url = api_url
|
| 41 |
+
self.num_agents = num_agents
|
| 42 |
+
self.calls_per_agent = calls_per_agent
|
| 43 |
+
self.special_config = special_config
|
| 44 |
+
self.agents = [Agent(api_url) for _ in range(num_agents)]
|
| 45 |
+
|
| 46 |
+
def run(self):
|
| 47 |
+
start_time = time.time()
|
| 48 |
+
for agent in self.agents:
|
| 49 |
+
for _ in range(self.calls_per_agent):
|
| 50 |
+
agent.make_api_call()
|
| 51 |
+
self.execution_time = time.time() - start_time
|
| 52 |
+
|
| 53 |
+
def process_data(self):
|
| 54 |
+
# This function will be implemented by GPT-3.5
|
| 55 |
+
pass
|
| 56 |
+
|
| 57 |
def execute_snn(api_url, openai_api_key, num_agents, calls_per_agent, special_config):
|
| 58 |
prompt = f"""
|
| 59 |
+
Implement the process_data method for the SwarmNeuralNetwork class. The method should:
|
| 60 |
+
1. Analyze the data collected by all agents (accessible via self.agents[i].data)
|
| 61 |
+
2. Generate a summary of the collected data
|
| 62 |
+
3. Derive insights from the collective behavior
|
| 63 |
+
4. Calculate performance metrics
|
| 64 |
+
5. Return a dictionary with keys 'data_summary', 'insights', and 'performance'
|
| 65 |
+
|
| 66 |
+
Consider the following parameters:
|
| 67 |
- API URL: {api_url}
|
| 68 |
- Number of Agents: {num_agents}
|
| 69 |
- Calls per Agent: {calls_per_agent}
|
| 70 |
- Special Configuration: {special_config if special_config else 'None'}
|
| 71 |
|
| 72 |
+
Provide only the Python code for the process_data method.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
"""
|
| 74 |
|
| 75 |
+
process_data_code = call_gpt3_5(prompt, openai_api_key)
|
| 76 |
|
| 77 |
+
if not process_data_code.startswith("Error"):
|
| 78 |
try:
|
| 79 |
+
# Create the SNN instance
|
| 80 |
+
snn = SwarmNeuralNetwork(api_url, num_agents, calls_per_agent, special_config)
|
| 81 |
+
|
| 82 |
+
# Add the process_data method to the SNN class
|
| 83 |
+
exec(process_data_code, globals())
|
| 84 |
+
SwarmNeuralNetwork.process_data = process_data
|
| 85 |
+
|
| 86 |
+
# Run the SNN
|
| 87 |
+
snn.run()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
+
# Process the data and get results
|
| 90 |
+
result = snn.process_data()
|
|
|
|
| 91 |
|
|
|
|
|
|
|
| 92 |
return f"Results from the swarm neural network:\n\n{json.dumps(result, indent=2)}"
|
| 93 |
except Exception as e:
|
| 94 |
+
return f"Error executing SNN: {str(e)}\n\nGenerated process_data code:\n{process_data_code}"
|
| 95 |
else:
|
| 96 |
+
return process_data_code
|
| 97 |
|
| 98 |
+
# Define the Gradio interface (same as before)
|
| 99 |
iface = gr.Interface(
|
| 100 |
fn=execute_snn,
|
| 101 |
inputs=[
|