Spaces:
Running
Running
add compare mode
Browse files- app.py +2 -0
- app_compare.py +210 -0
app.py
CHANGED
|
@@ -26,12 +26,14 @@ from app_openai_voice import demo as demo_openai_voice
|
|
| 26 |
from app_qwen import demo as demo_qwen
|
| 27 |
from app_deepseek import demo as demo_deepseek
|
| 28 |
from app_crew import demo as demo_crew
|
|
|
|
| 29 |
from app_hyperbolic import demo as demo_hyperbolic
|
| 30 |
from utils import get_app
|
| 31 |
|
| 32 |
# Create mapping of providers to their demos
|
| 33 |
PROVIDERS = {
|
| 34 |
"DeepSeek": demo_deepseek,
|
|
|
|
| 35 |
"Qwen" : demo_qwen,
|
| 36 |
"Gemini": demo_gemini,
|
| 37 |
"OpenAI Voice": demo_openai_voice,
|
|
|
|
| 26 |
from app_qwen import demo as demo_qwen
|
| 27 |
from app_deepseek import demo as demo_deepseek
|
| 28 |
from app_crew import demo as demo_crew
|
| 29 |
+
from app_compare import demo as demo_compare
|
| 30 |
from app_hyperbolic import demo as demo_hyperbolic
|
| 31 |
from utils import get_app
|
| 32 |
|
| 33 |
# Create mapping of providers to their demos
|
| 34 |
PROVIDERS = {
|
| 35 |
"DeepSeek": demo_deepseek,
|
| 36 |
+
"Compare": demo_compare,
|
| 37 |
"Qwen" : demo_qwen,
|
| 38 |
"Gemini": demo_gemini,
|
| 39 |
"OpenAI Voice": demo_openai_voice,
|
app_compare.py
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
+
|
| 5 |
+
import google.generativeai as genai
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import openai
|
| 8 |
+
from anthropic import Anthropic
|
| 9 |
+
from openai import OpenAI # Add explicit OpenAI import
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def get_all_models():
|
| 13 |
+
"""Get all available models from the registries."""
|
| 14 |
+
return [
|
| 15 |
+
"SambaNova: Meta-Llama-3.2-1B-Instruct",
|
| 16 |
+
"SambaNova: Meta-Llama-3.2-3B-Instruct",
|
| 17 |
+
"SambaNova: Llama-3.2-11B-Vision-Instruct",
|
| 18 |
+
"SambaNova: Llama-3.2-90B-Vision-Instruct",
|
| 19 |
+
"SambaNova: Meta-Llama-3.1-8B-Instruct",
|
| 20 |
+
"SambaNova: Meta-Llama-3.1-70B-Instruct",
|
| 21 |
+
"SambaNova: Meta-Llama-3.1-405B-Instruct",
|
| 22 |
+
"Hyperbolic: Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 23 |
+
"Hyperbolic: meta-llama/Llama-3.2-3B-Instruct",
|
| 24 |
+
"Hyperbolic: meta-llama/Meta-Llama-3.1-8B-Instruct",
|
| 25 |
+
"Hyperbolic: meta-llama/Meta-Llama-3.1-70B-Instruct",
|
| 26 |
+
"Hyperbolic: meta-llama/Meta-Llama-3-70B-Instruct",
|
| 27 |
+
"Hyperbolic: NousResearch/Hermes-3-Llama-3.1-70B",
|
| 28 |
+
"Hyperbolic: Qwen/Qwen2.5-72B-Instruct",
|
| 29 |
+
"Hyperbolic: deepseek-ai/DeepSeek-V2.5",
|
| 30 |
+
"Hyperbolic: meta-llama/Meta-Llama-3.1-405B-Instruct",
|
| 31 |
+
]
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def generate_discussion_prompt(original_question: str, previous_responses: List[str]) -> str:
|
| 35 |
+
"""Generate a prompt for models to discuss and build upon previous
|
| 36 |
+
responses."""
|
| 37 |
+
prompt = f"""You are participating in a multi-AI discussion about this question: "{original_question}"
|
| 38 |
+
|
| 39 |
+
Previous responses from other AI models:
|
| 40 |
+
{chr(10).join(f"- {response}" for response in previous_responses)}
|
| 41 |
+
|
| 42 |
+
Please provide your perspective while:
|
| 43 |
+
1. Acknowledging key insights from previous responses
|
| 44 |
+
2. Adding any missing important points
|
| 45 |
+
3. Respectfully noting if you disagree with anything and explaining why
|
| 46 |
+
4. Building towards a complete answer
|
| 47 |
+
|
| 48 |
+
Keep your response focused and concise (max 3-4 paragraphs)."""
|
| 49 |
+
return prompt
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def generate_consensus_prompt(original_question: str, discussion_history: List[str]) -> str:
|
| 53 |
+
"""Generate a prompt for final consensus building."""
|
| 54 |
+
return f"""Review this multi-AI discussion about: "{original_question}"
|
| 55 |
+
|
| 56 |
+
Discussion history:
|
| 57 |
+
{chr(10).join(discussion_history)}
|
| 58 |
+
|
| 59 |
+
As a final synthesizer, please:
|
| 60 |
+
1. Identify the key points where all models agreed
|
| 61 |
+
2. Explain how any disagreements were resolved
|
| 62 |
+
3. Present a clear, unified answer that represents our collective best understanding
|
| 63 |
+
4. Note any remaining uncertainties or caveats
|
| 64 |
+
|
| 65 |
+
Keep the final consensus concise but complete."""
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def chat_with_openai(model: str, messages: List[Dict], api_key: str | None) -> str:
|
| 69 |
+
import openai
|
| 70 |
+
|
| 71 |
+
client = openai.OpenAI(api_key=api_key)
|
| 72 |
+
response = client.chat.completions.create(model=model, messages=messages)
|
| 73 |
+
return response.choices[0].message.content
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def chat_with_anthropic(messages: List[Dict], api_key: str | None) -> str:
|
| 77 |
+
"""Chat with Anthropic's Claude model."""
|
| 78 |
+
client = Anthropic(api_key=api_key)
|
| 79 |
+
response = client.messages.create(model="claude-3-sonnet-20240229", messages=messages, max_tokens=1024)
|
| 80 |
+
return response.content[0].text
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def chat_with_gemini(messages: List[Dict], api_key: str | None) -> str:
|
| 84 |
+
"""Chat with Gemini Pro model."""
|
| 85 |
+
genai.configure(api_key=api_key)
|
| 86 |
+
model = genai.GenerativeModel("gemini-pro")
|
| 87 |
+
|
| 88 |
+
# Convert messages to Gemini format
|
| 89 |
+
gemini_messages = []
|
| 90 |
+
for msg in messages:
|
| 91 |
+
role = "user" if msg["role"] == "user" else "model"
|
| 92 |
+
gemini_messages.append({"role": role, "parts": [msg["content"]]})
|
| 93 |
+
|
| 94 |
+
response = model.generate_content([m["parts"][0] for m in gemini_messages])
|
| 95 |
+
return response.text
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
def chat_with_sambanova(
|
| 99 |
+
messages: List[Dict], api_key: str | None, model_name: str = "Llama-3.2-90B-Vision-Instruct"
|
| 100 |
+
) -> str:
|
| 101 |
+
"""Chat with SambaNova's models using their OpenAI-compatible API."""
|
| 102 |
+
client = openai.OpenAI(
|
| 103 |
+
api_key=api_key,
|
| 104 |
+
base_url="https://api.sambanova.ai/v1",
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
response = client.chat.completions.create(
|
| 108 |
+
model=model_name, messages=messages, temperature=0.1, top_p=0.1 # Use the specific model name passed in
|
| 109 |
+
)
|
| 110 |
+
return response.choices[0].message.content
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
def chat_with_hyperbolic(
|
| 114 |
+
messages: List[Dict], api_key: str | None, model_name: str = "Qwen/Qwen2.5-Coder-32B-Instruct"
|
| 115 |
+
) -> str:
|
| 116 |
+
"""Chat with Hyperbolic's models using their OpenAI-compatible API."""
|
| 117 |
+
client = OpenAI(api_key=api_key, base_url="https://api.hyperbolic.xyz/v1")
|
| 118 |
+
|
| 119 |
+
# Add system message to the start of the messages list
|
| 120 |
+
full_messages = [
|
| 121 |
+
{"role": "system", "content": "You are a helpful assistant. Be descriptive and clear."},
|
| 122 |
+
*messages,
|
| 123 |
+
]
|
| 124 |
+
|
| 125 |
+
response = client.chat.completions.create(
|
| 126 |
+
model=model_name, # Use the specific model name passed in
|
| 127 |
+
messages=full_messages,
|
| 128 |
+
temperature=0.7,
|
| 129 |
+
max_tokens=1024,
|
| 130 |
+
)
|
| 131 |
+
return response.choices[0].message.content
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
def multi_model_consensus(
|
| 135 |
+
question: str, selected_models: List[str], rounds: int = 3, progress: gr.Progress = gr.Progress()
|
| 136 |
+
) -> list[tuple[str, str]]:
|
| 137 |
+
if not selected_models:
|
| 138 |
+
raise gr.Error("Please select at least one model to chat with.")
|
| 139 |
+
|
| 140 |
+
chat_history = []
|
| 141 |
+
progress(0, desc="Getting responses from all models...")
|
| 142 |
+
|
| 143 |
+
# Get responses from all models in parallel
|
| 144 |
+
for i, model in enumerate(selected_models):
|
| 145 |
+
provider, model_name = model.split(": ", 1)
|
| 146 |
+
progress((i + 1) / len(selected_models), desc=f"Getting response from {model}...")
|
| 147 |
+
|
| 148 |
+
try:
|
| 149 |
+
if provider == "Anthropic":
|
| 150 |
+
api_key = os.getenv("ANTHROPIC_API_KEY")
|
| 151 |
+
response = chat_with_anthropic(messages=[{"role": "user", "content": question}], api_key=api_key)
|
| 152 |
+
elif provider == "SambaNova":
|
| 153 |
+
api_key = os.getenv("SAMBANOVA_API_KEY")
|
| 154 |
+
response = chat_with_sambanova(
|
| 155 |
+
messages=[
|
| 156 |
+
{"role": "system", "content": "You are a helpful assistant"},
|
| 157 |
+
{"role": "user", "content": question},
|
| 158 |
+
],
|
| 159 |
+
api_key=api_key,
|
| 160 |
+
model_name=model_name,
|
| 161 |
+
)
|
| 162 |
+
elif provider == "Hyperbolic":
|
| 163 |
+
api_key = os.getenv("HYPERBOLIC_API_KEY")
|
| 164 |
+
response = chat_with_hyperbolic(
|
| 165 |
+
messages=[{"role": "user", "content": question}],
|
| 166 |
+
api_key=api_key,
|
| 167 |
+
model_name=model_name,
|
| 168 |
+
)
|
| 169 |
+
else: # Gemini
|
| 170 |
+
api_key = os.getenv("GEMINI_API_KEY")
|
| 171 |
+
response = chat_with_gemini(messages=[{"role": "user", "content": question}], api_key=api_key)
|
| 172 |
+
|
| 173 |
+
chat_history.append((model, response))
|
| 174 |
+
except Exception as e:
|
| 175 |
+
chat_history.append((model, f"Error: {str(e)}"))
|
| 176 |
+
|
| 177 |
+
progress(1.0, desc="Done!")
|
| 178 |
+
return chat_history
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
with gr.Blocks() as demo:
|
| 182 |
+
gr.Markdown("# Model Response Comparison")
|
| 183 |
+
gr.Markdown(
|
| 184 |
+
"""Select multiple models to compare their responses"""
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
+
with gr.Row():
|
| 188 |
+
with gr.Column():
|
| 189 |
+
model_selector = gr.Dropdown(
|
| 190 |
+
choices=get_all_models(),
|
| 191 |
+
multiselect=True,
|
| 192 |
+
label="Select Models",
|
| 193 |
+
info="Choose models to compare",
|
| 194 |
+
value=["SambaNova: Llama-3.2-90B-Vision-Instruct", "Hyperbolic: Qwen/Qwen2.5-Coder-32B-Instruct"],
|
| 195 |
+
)
|
| 196 |
+
|
| 197 |
+
chatbot = gr.Chatbot(height=600, label="Model Responses")
|
| 198 |
+
msg = gr.Textbox(label="Prompt", placeholder="Ask a question to compare model responses...")
|
| 199 |
+
|
| 200 |
+
def respond(message, selected_models):
|
| 201 |
+
chat_history = multi_model_consensus(message, selected_models, rounds=1)
|
| 202 |
+
return chat_history
|
| 203 |
+
|
| 204 |
+
msg.submit(respond, [msg, model_selector], [chatbot])
|
| 205 |
+
|
| 206 |
+
for fn in demo.fns.values():
|
| 207 |
+
fn.api_name = False
|
| 208 |
+
|
| 209 |
+
if __name__ == "__main__":
|
| 210 |
+
demo.launch()
|