import gradio as gr from langchain_openai import ChatOpenAI # from dspy import Agent # Base class for custom agent # from dspy import spawn_processes # Distributed computing utility from transformers import pipeline # Choose model model_name = "Dolphin-Phi" # Load the chosen LLM model llm = pipeline("text-generation", model=Dolphin-Phi) # DSPy-based prompt generation from dspy.agents import Agent from dspy.utils import SentenceSplitter, SentimentAnalyzer, NamedEntityRecognizer def dspy_generate_agent_prompts(prompt): """ Generates prompts for different agents based on the provided prompt and DSPy functionalities. Args: prompt (str): The user-provided prompt (e.g., customer reviews). Returns: list: A list containing agent-specific prompts. """ # 1. Split the prompt into individual sentences sentences = SentenceSplitter().process(prompt) # 2. Analyze sentiment for each sentence sentiment_analyzer = SentimentAnalyzer() sentiment_labels = [] for sentence in sentences: sentiment_labels.append(sentiment_analyzer.analyze(sentence)) # 3. Extract named entities related to specific topics ner = NamedEntityRecognizer(model_name="en_core_web_sm") extracted_entities = {} for sentence in sentences: entities = ner.process(sentence) for entity in entities: if entity.label_ in ["FOOD", "ORG", "LOCATION"]: # Customize entity labels based on your needs extracted_entities.setdefault(entity.label_, []).append(entity.text) # 4. Craft prompts for each agent agent_prompts = [] # **Sentiment Analyzer Prompt:** sentiment_prompt = f"Analyze the sentiment of the following sentences:\n" + "\n".join(sentences) agent_prompts.append(sentiment_prompt) # **Topic Extractor Prompt:** (Modify based on your specific topics) topic_prompt = f"Extract the main topics discussed in the following text, focusing on food, service, and ambiance:\n{prompt}" agent_prompts.append(topic_prompt) # **Recommendation Generator Prompt:** (Modify based on your requirements) positive_count = sum(label == "POSITIVE" for label in sentiment_labels) negative_count = sum(label == "NEGATIVE" for label in sentiment_labels) neutral_count = sum(label == "NEUTRAL" for label in sentiment_labels) topic_mentions = "\n".join(f"{k}: {','.join(v)}" for k, v in extracted_entities.items()) 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.""" agent_prompts.append(recommendation_prompt) return agent_prompts # Define the main function to be used with Gradio def generate_outputs(user_prompt): # 1. Process prompt with langchain (replace with your actual implementation) processed_prompt = langchain_function(user_prompt) # Replace with your langchain logic # 2. Generate synthetic data using DSPy's distributed computing capabilities synthetic_data = generate_synthetic_data_distributed(processed_prompt) # 3. Combine user prompt and synthetic data combined_data = f"{user_prompt}\n{synthetic_data}" # 4. Generate prompts for agents using DSPy agent_prompts = dspy_generate_agent_prompts(processed_prompt) # 5. Use the chosen LLM for two of the prompts output_1 = llm(agent_prompts[0], max_length=100)[0]["generated_text"] output_2 = llm(agent_prompts[1], max_length=100)[0]["generated_text"] # 6. Produce outputs with Langchain or DSPy (replace with your actual implementation) report, recommendations, visualization = produce_outputs(combined_data) return report, recommendations, visualization # Create the Gradio interface gr.Interface( fn=generate_outputs, inputs=gr.Textbox(label="Enter a prompt"), outputs=["textbox", "textbox", "image"], title="Multi-Agent Prompt Processor", description="Processes a prompt using Langchain, DSPy, and a chosen Hugging Face LLM to generate diverse outputs.", ).launch()