Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
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)
|
| 10 |
+
try:
|
| 11 |
+
response = client.chat.completions.create(
|
| 12 |
+
model="gpt-3.5-turbo",
|
| 13 |
+
messages=[
|
| 14 |
+
{"role": "system", "content": "You are a helpful assistant capable of constructing and executing a Swarm Neural Network (SNN). Return only the formatted results of the SNN execution."},
|
| 15 |
+
{"role": "user", "content": prompt}
|
| 16 |
+
]
|
| 17 |
+
)
|
| 18 |
+
return response.choices[0].message.content
|
| 19 |
+
except Exception as e:
|
| 20 |
+
return f"Error calling GPT-3.5: {str(e)}"
|
| 21 |
+
|
| 22 |
+
def execute_snn(api_url, openai_api_key, num_agents, calls_per_agent, special_config):
|
| 23 |
+
prompt = f"""
|
| 24 |
+
Construct and execute a Swarm Neural Network (SNN) with the following parameters:
|
| 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 |
+
Simulate the execution of this SNN and provide only the formatted results. The results should include:
|
| 31 |
+
1. A summary of the data retrieved from the API calls
|
| 32 |
+
2. Any patterns or insights derived from the collective behavior of the agents
|
| 33 |
+
3. Performance metrics of the SNN (e.g., execution time, success rate of API calls)
|
| 34 |
+
|
| 35 |
+
Present the results in a clear, structured format without any additional explanations or descriptions of the SNN process.
|
| 36 |
+
"""
|
| 37 |
+
|
| 38 |
+
gpt_response = call_gpt3_5(prompt, openai_api_key)
|
| 39 |
+
|
| 40 |
+
if gpt_response:
|
| 41 |
+
return f"Results from the swarm neural network:\n\n{gpt_response}"
|
| 42 |
+
else:
|
| 43 |
+
return "Failed to execute SNN due to GPT-3.5 API call failure."
|
| 44 |
+
|
| 45 |
+
# Define the Gradio interface
|
| 46 |
+
iface = gr.Interface(
|
| 47 |
+
fn=execute_snn,
|
| 48 |
+
inputs=[
|
| 49 |
+
gr.Textbox(label="API URL for your task"),
|
| 50 |
+
gr.Textbox(label="OpenAI API Key", type="password"),
|
| 51 |
+
gr.Number(label="Number of Agents", minimum=1, maximum=100, step=1),
|
| 52 |
+
gr.Number(label="Calls per Agent", minimum=1, maximum=100, step=1),
|
| 53 |
+
gr.Textbox(label="Special Configuration (optional)")
|
| 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"]
|
| 61 |
+
]
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
# Launch the interface
|
| 65 |
+
iface.launch()
|