OVERHAUL
Browse files
app.py
CHANGED
@@ -1,133 +1,98 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
# This may be in the wrong spot
|
4 |
-
def generate_outputs(user_prompt):
|
5 |
-
report, recommendations, visualization = produce_outputs(combined_data)
|
6 |
-
return report, recommendations, visualization
|
7 |
-
|
8 |
from langchain_openai import TextProcessingAgent
|
9 |
from dspy.agents import Agent # Base class for custom agent
|
10 |
from dspy.utils import spawn_processes # Distributed computing utility
|
|
|
11 |
|
12 |
-
#
|
13 |
-
|
14 |
-
|
15 |
|
16 |
-
#
|
|
|
17 |
|
|
|
|
|
|
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
return response.choices[0].text
|
23 |
|
|
|
|
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
super().__init__()
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
processed_data = data.lower().strip()
|
33 |
-
return processed_data
|
34 |
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
-
#
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
41 |
|
|
|
|
|
42 |
|
43 |
-
#
|
44 |
-
|
|
|
45 |
|
46 |
-
#
|
47 |
-
|
48 |
-
|
49 |
-
"""
|
50 |
-
Initialize with configurations for teams.
|
51 |
-
"""
|
52 |
-
self.teams = {team_name: self.Team(team_agents) for team_name, team_agents in teams_config.items()}
|
53 |
-
self.states = ["waiting", "interacting", "finalizing"]
|
54 |
-
self.current_state = "waiting"
|
55 |
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
self.current_state = to_state
|
62 |
-
else:
|
63 |
-
raise ValueError("Invalid state transition attempted.")
|
64 |
|
65 |
-
|
66 |
-
|
67 |
-
Broadcast a message to all teams based on the current FSM state.
|
68 |
-
"""
|
69 |
-
if self.current_state == "interacting":
|
70 |
-
responses = {team_name: team.broadcast(message) for team_name, team in self.teams.items()}
|
71 |
-
return responses
|
72 |
-
else:
|
73 |
-
return "The group chat is not in an interacting state."
|
74 |
|
75 |
-
|
76 |
-
def __init__(self, agents_config):
|
77 |
-
self.agents = [self.Agent(agent_config) for agent_config in agents_config]
|
78 |
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
class Agent:
|
84 |
-
def __init__(self, config):
|
85 |
-
self.agent_name = config['agent_name']
|
86 |
-
self.api_key = config['api_key']
|
87 |
-
self.model = config['model']
|
88 |
-
|
89 |
-
def respond(self, message):
|
90 |
-
return f"{self.agent_name} responding with {self.model}"
|
91 |
|
92 |
-
#
|
93 |
-
|
94 |
-
# Use Langchain for LLM-based analysis, recommendations, etc. Should this be updated to DSPy too? again:[TONIC PLEASE HELP LOL]
|
95 |
-
analysis = openai.complete(prompt=f"Analyze {processed_data}", engine="text-davinci-003", max_tokens=200)
|
96 |
-
recommendations = openai.complete(prompt=f"Recommend strategies based on {processed_data}", engine="text-davinci-003", max_tokens=100)
|
97 |
-
# Replace with your visualization logic
|
98 |
-
visualization = None
|
99 |
-
return analysis.choices[0].text, recommendations.choices[0].text, visualization
|
100 |
|
|
|
|
|
101 |
|
102 |
-
#
|
103 |
-
|
104 |
-
# Spawn synthetic data generation processes across multiple nodes
|
105 |
-
processes = [spawn_processes(generate_synthetic_data, [f"Simulate scenarios for {prompt}"]) for _ in range(num_nodes)]
|
106 |
|
107 |
-
#
|
108 |
-
|
109 |
-
|
110 |
-
synthetic_data_list.extend(process.get())
|
111 |
|
112 |
-
#
|
113 |
-
|
114 |
|
|
|
115 |
|
116 |
-
#
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
# Print the results for testing pyrposes
|
125 |
-
print("Report:")
|
126 |
-
print(report)
|
127 |
-
print("\nRecommendations:")
|
128 |
-
print(recommendations)
|
129 |
-
print("\nVisualization:")
|
130 |
-
print(visualization) # Currently "None" due to placeholder 'visualization'
|
131 |
-
|
132 |
-
# Moved from other gradio chunk near the top
|
133 |
-
gr.Interface(fn=generate_outputs, inputs=user_prompt, outputs=["text", "text", "image"]).launch()
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
from langchain_openai import TextProcessingAgent
|
3 |
from dspy.agents import Agent # Base class for custom agent
|
4 |
from dspy.utils import spawn_processes # Distributed computing utility
|
5 |
+
from transformers import pipeline
|
6 |
|
7 |
+
# Choose model
|
8 |
+
model_name = "Dolphin-Phi"
|
|
|
9 |
|
10 |
+
# Load the chosen LLM model
|
11 |
+
llm = pipeline("text-generation", model=Dolphin-Phi)
|
12 |
|
13 |
+
# DSPy-based prompt generation
|
14 |
+
from dspy.agents import Agent
|
15 |
+
from dspy.utils import SentenceSplitter, SentimentAnalyzer, NamedEntityRecognizer
|
16 |
|
17 |
+
def dspy_generate_agent_prompts(prompt):
|
18 |
+
"""
|
19 |
+
Generates prompts for different agents based on the provided prompt and DSPy functionalities.
|
|
|
20 |
|
21 |
+
Args:
|
22 |
+
prompt (str): The user-provided prompt (e.g., customer reviews).
|
23 |
|
24 |
+
Returns:
|
25 |
+
list: A list containing agent-specific prompts.
|
26 |
+
"""
|
|
|
27 |
|
28 |
+
# 1. Split the prompt into individual sentences
|
29 |
+
sentences = SentenceSplitter().process(prompt)
|
|
|
|
|
30 |
|
31 |
+
# 2. Analyze sentiment for each sentence
|
32 |
+
sentiment_analyzer = SentimentAnalyzer()
|
33 |
+
sentiment_labels = []
|
34 |
+
for sentence in sentences:
|
35 |
+
sentiment_labels.append(sentiment_analyzer.analyze(sentence))
|
36 |
|
37 |
+
# 3. Extract named entities related to specific topics
|
38 |
+
ner = NamedEntityRecognizer(model_name="en_core_web_sm")
|
39 |
+
extracted_entities = {}
|
40 |
+
for sentence in sentences:
|
41 |
+
entities = ner.process(sentence)
|
42 |
+
for entity in entities:
|
43 |
+
if entity.label_ in ["FOOD", "ORG", "LOCATION"]: # Customize entity labels based on your needs
|
44 |
+
extracted_entities.setdefault(entity.label_, []).append(entity.text)
|
45 |
|
46 |
+
# 4. Craft prompts for each agent
|
47 |
+
agent_prompts = []
|
48 |
|
49 |
+
# **Sentiment Analyzer Prompt:**
|
50 |
+
sentiment_prompt = f"Analyze the sentiment of the following sentences:\n" + "\n".join(sentences)
|
51 |
+
agent_prompts.append(sentiment_prompt)
|
52 |
|
53 |
+
# **Topic Extractor Prompt:** (Modify based on your specific topics)
|
54 |
+
topic_prompt = f"Extract the main topics discussed in the following text, focusing on food, service, and ambiance:\n{prompt}"
|
55 |
+
agent_prompts.append(topic_prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
+
# **Recommendation Generator Prompt:** (Modify based on your requirements)
|
58 |
+
positive_count = sum(label == "POSITIVE" for label in sentiment_labels)
|
59 |
+
negative_count = sum(label == "NEGATIVE" for label in sentiment_labels)
|
60 |
+
neutral_count = sum(label == "NEUTRAL" for label in sentiment_labels)
|
61 |
+
topic_mentions = "\n".join(f"{k}: {','.join(v)}" for k, v in extracted_entities.items())
|
|
|
|
|
|
|
62 |
|
63 |
+
recommendation_prompt = f"""Based on the sentiment analysis (positive: {positive_count}, negative: {negative_count}, neutral: {neutral_count}) and extracted topics ({topic_mentions}), suggest recommendations for the restaurant to improve."""
|
64 |
+
agent_prompts.append(recommendation_prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
+
return agent_prompts
|
|
|
|
|
67 |
|
68 |
+
# Define the main function to be used with Gradio
|
69 |
+
def generate_outputs(user_prompt):
|
70 |
+
# 1. Process prompt with langchain (replace with your actual implementation)
|
71 |
+
processed_prompt = langchain_function(user_prompt) # Replace with your langchain logic
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
+
# 2. Generate synthetic data using DSPy's distributed computing capabilities
|
74 |
+
synthetic_data = generate_synthetic_data_distributed(processed_prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
+
# 3. Combine user prompt and synthetic data
|
77 |
+
combined_data = f"{user_prompt}\n{synthetic_data}"
|
78 |
|
79 |
+
# 4. Generate prompts for agents using DSPy
|
80 |
+
agent_prompts = dspy_generate_agent_prompts(processed_prompt)
|
|
|
|
|
81 |
|
82 |
+
# 5. Use the chosen LLM for two of the prompts
|
83 |
+
output_1 = llm(agent_prompts[0], max_length=100)[0]["generated_text"]
|
84 |
+
output_2 = llm(agent_prompts[1], max_length=100)[0]["generated_text"]
|
|
|
85 |
|
86 |
+
# 6. Produce outputs with Langchain or DSPy (replace with your actual implementation)
|
87 |
+
report, recommendations, visualization = produce_outputs(combined_data)
|
88 |
|
89 |
+
return report, recommendations, visualization
|
90 |
|
91 |
+
# Create the Gradio interface
|
92 |
+
gr.Interface(
|
93 |
+
fn=generate_outputs,
|
94 |
+
inputs=gr.Textbox(label="Enter a prompt"),
|
95 |
+
outputs=["textbox", "textbox", "image"],
|
96 |
+
title="Multi-Agent Prompt Processor",
|
97 |
+
description="Processes a prompt using Langchain, DSPy, and a chosen Hugging Face LLM to generate diverse outputs.",
|
98 |
+
).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|