Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import os | |
| import json | |
| import numpy as np | |
| import requests | |
| from openai import OpenAI | |
| def call_gpt3_5(prompt, api_key): | |
| client = OpenAI(api_key=api_key) | |
| try: | |
| response = client.chat.completions.create( | |
| model="gpt-3.5-turbo", | |
| messages=[ | |
| {"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."}, | |
| {"role": "user", "content": prompt} | |
| ] | |
| ) | |
| return response.choices[0].message.content | |
| except Exception as e: | |
| return f"Error calling GPT-3.5: {str(e)}" | |
| def execute_snn(api_url, openai_api_key, num_agents, calls_per_agent, special_config): | |
| prompt = f""" | |
| Construct and execute a Swarm Neural Network (SNN) with the following parameters: | |
| - API URL: {api_url} | |
| - Number of Agents: {num_agents} | |
| - Calls per Agent: {calls_per_agent} | |
| - Special Configuration: {special_config if special_config else 'None'} | |
| Simulate the execution of this SNN and provide only the formatted results. The results should include: | |
| 1. A summary of the data retrieved from the API calls | |
| 2. Any patterns or insights derived from the collective behavior of the agents | |
| 3. Performance metrics of the SNN (e.g., execution time, success rate of API calls) | |
| Present the results in a clear, structured format without any additional explanations or descriptions of the SNN process. | |
| """ | |
| gpt_response = call_gpt3_5(prompt, openai_api_key) | |
| if gpt_response: | |
| return f"Results from the swarm neural network:\n\n{gpt_response}" | |
| else: | |
| return "Failed to execute SNN due to GPT-3.5 API call failure." | |
| # Define the Gradio interface | |
| iface = gr.Interface( | |
| fn=execute_snn, | |
| inputs=[ | |
| gr.Textbox(label="API URL for your task"), | |
| gr.Textbox(label="OpenAI API Key", type="password"), | |
| gr.Number(label="Number of Agents", minimum=1, maximum=100, step=1), | |
| gr.Number(label="Calls per Agent", minimum=1, maximum=100, step=1), | |
| gr.Textbox(label="Special Configuration (optional)") | |
| ], | |
| outputs="text", | |
| title="Swarm Neural Network Simulator", | |
| description="Enter the parameters for your Swarm Neural Network (SNN) simulation.", | |
| examples=[ | |
| ["https://meowfacts.herokuapp.com/", "your-api-key-here", 3, 1, ""], | |
| ["https://api.publicapis.org/entries", "your-api-key-here", 5, 2, "category=Animals"] | |
| ] | |
| ) | |
| # Launch the interface | |
| iface.launch() |