add experimental tab
Browse filesFiles changed (2)
- app.py+4
- app_experimental.py+162-88
15 unmodified lines161718192021222324252636 unmodified lines6364656667686915 unmodified linesfrom app_xai import demo as demo_grokfrom app_flux import demo as demo_fluxfrom app_ltx_video import demo as demo_ltx_videofrom app_experimental import demo as demo_experimental
with gr.Blocks(fill_height=True) as demo: with gr.Tab("Experimental"): demo_experimental.render() with gr.Tab("Meta Llama"): demo_sambanova.render() gr.Markdown(36 unmodified lines demo_nvidia.render() with gr.Tab("Flux"): demo_flux.render()
if __name__ == "__main__":12334567891011121314151617181920212223242526272829303132333435363738395678910111244451314151617181920212223242526272829303136 unmodified lines686970888971727391927494957576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213311 unmodified lines14514614711614811811912014912212312412512612712812915013115115215313313413515415515615715815916016116216316416516616716816917013713813914017117217317417517617717810 unmodified lines1891901911571921591601611931941951641651661671681961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272281752292301772312321802332342352361841851861871882372382392402412422432442452462472482492502512522532542552562572582592602612622632642656 unmodified lines272273274201275276277278279280281208209210211282283284285286287288215216289290291292293import osimport gradio as grfrom typing import List, Dictfrom typing import List, Dict, Callableimport randomimport timefrom utils import get_app
# Import all the model registries (keeping existing imports)import anthropic_gradioimport cerebras_gradioimport dashscope_gradioimport fireworks_gradioimport gemini_gradioimport groq_gradioimport hyperbolic_gradioimport mistral_gradioimport nvidia_gradioimport openai_gradioimport perplexity_gradioimport sambanova_gradioimport together_gradioimport xai_gradio
# Define MODEL_REGISTRIES dictionaryMODEL_REGISTRIES = { "OpenAI": (openai_gradio.registry, os.getenv("OPENAI_API_KEY")), "Anthropic": (anthropic_gradio.registry, os.getenv("ANTHROPIC_API_KEY")), "Cerebras": (cerebras_gradio, os.getenv("CEREBRAS_API_KEY")), "DashScope": (dashscope_gradio, os.getenv("DASHSCOPE_API_KEY")), "Fireworks": (fireworks_gradio, os.getenv("FIREWORKS_API_KEY")), "Gemini": (gemini_gradio, os.getenv("GEMINI_API_KEY")), "Groq": (groq_gradio, os.getenv("GROQ_API_KEY")), "Hyperbolic": (hyperbolic_gradio, os.getenv("HYPERBOLIC_API_KEY")), "Mistral": (mistral_gradio, os.getenv("MISTRAL_API_KEY")), "NVIDIA": (nvidia_gradio, os.getenv("NVIDIA_API_KEY")), "SambaNova": (sambanova_gradio, os.getenv("SAMBANOVA_API_KEY")), "Together": (together_gradio, os.getenv("TOGETHER_API_KEY")), "XAI": (xai_gradio, os.getenv("XAI_API_KEY")),}import google.generativeai as genaifrom anthropic import Anthropicimport openaifrom openai import OpenAI # Add explicit OpenAI import
def get_all_models(): """Get all available models from the registries.""" return [ "OpenAI: gpt-4o", # From app_openai.py "Anthropic: claude-3-5-sonnet-20241022", # From app_claude.py "SambaNova: Meta-Llama-3.2-1B-Instruct", "SambaNova: Meta-Llama-3.2-3B-Instruct", "SambaNova: Llama-3.2-11B-Vision-Instruct", "SambaNova: Llama-3.2-90B-Vision-Instruct", "SambaNova: Meta-Llama-3.1-8B-Instruct", "SambaNova: Meta-Llama-3.1-70B-Instruct", "SambaNova: Meta-Llama-3.1-405B-Instruct", "Hyperbolic: Qwen/Qwen2.5-Coder-32B-Instruct", "Hyperbolic: meta-llama/Llama-3.2-3B-Instruct", "Hyperbolic: meta-llama/Meta-Llama-3.1-8B-Instruct", "Hyperbolic: meta-llama/Meta-Llama-3.1-70B-Instruct", "Hyperbolic: meta-llama/Meta-Llama-3-70B-Instruct", "Hyperbolic: NousResearch/Hermes-3-Llama-3.1-70B", "Hyperbolic: Qwen/Qwen2.5-72B-Instruct", "Hyperbolic: deepseek-ai/DeepSeek-V2.5", "Hyperbolic: meta-llama/Meta-Llama-3.1-405B-Instruct", ]
def generate_discussion_prompt(original_question: str, previous_responses: List[str]) -> str:36 unmodified lines ) return response.choices[0].message.content
def chat_with_anthropic(model: str, messages: List[Dict], api_key: str) -> str: from anthropic import Anthropicdef chat_with_anthropic(messages: List[Dict], api_key: str) -> str: """Chat with Anthropic's Claude model.""" client = Anthropic(api_key=api_key) # Convert messages to Anthropic format prompt = "\n\n".join([f"{m['role']}: {m['content']}" for m in messages]) response = client.messages.create( model=model, messages=[{"role": "user", "content": prompt}] model="claude-3-sonnet-20240229", messages=messages, max_tokens=1024 ) return response.content[0].text
def chat_with_gemini(messages: List[Dict], api_key: str) -> str: """Chat with Gemini Pro model.""" genai.configure(api_key=api_key) model = genai.GenerativeModel('gemini-pro') # Convert messages to Gemini format gemini_messages = [] for msg in messages: role = "user" if msg["role"] == "user" else "model" gemini_messages.append({"role": role, "parts": [msg["content"]]}) response = model.generate_content([m["parts"][0] for m in gemini_messages]) return response.text
def chat_with_sambanova(messages: List[Dict], api_key: str, model_name: str = "Llama-3.2-90B-Vision-Instruct") -> str: """Chat with SambaNova's models using their OpenAI-compatible API.""" client = openai.OpenAI( api_key=api_key, base_url="https://api.sambanova.ai/v1", ) response = client.chat.completions.create( model=model_name, # Use the specific model name passed in messages=messages, temperature=0.1, top_p=0.1 ) return response.choices[0].message.content
def chat_with_hyperbolic(messages: List[Dict], api_key: str, model_name: str = "Qwen/Qwen2.5-Coder-32B-Instruct") -> str: """Chat with Hyperbolic's models using their OpenAI-compatible API.""" client = OpenAI( api_key=api_key, base_url="https://api.hyperbolic.xyz/v1" ) # Add system message to the start of the messages list full_messages = [ {"role": "system", "content": "You are a helpful assistant. Be descriptive and clear."}, *messages ] response = client.chat.completions.create( model=model_name, # Use the specific model name passed in messages=full_messages, temperature=0.7, max_tokens=1024, ) return response.choices[0].message.content
def multi_model_consensus( question: str, selected_models: List[str], 11 unmodified lines initial_responses = [] for i, model in enumerate(selected_models): provider, model_name = model.split(": ", 1) registry_fn, api_key = MODEL_REGISTRIES[provider] if not api_key: continue try: # Load the model using the registry function predictor = gr.load( name=model_name, src=registry_fn, token=api_key ) # Format the request based on the provider if provider == "Anthropic": response = predictor.predict( api_key = os.getenv("ANTHROPIC_API_KEY") response = chat_with_anthropic( messages=[{"role": "user", "content": question}], max_tokens=1024, model=model_name, api_name="chat" api_key=api_key ) elif provider == "SambaNova": api_key = os.getenv("SAMBANOVA_API_KEY") response = chat_with_sambanova( messages=[ {"role": "system", "content": "You are a helpful assistant"}, {"role": "user", "content": question} ], api_key=api_key ) elif provider == "Hyperbolic": # Add Hyperbolic case api_key = os.getenv("HYPERBOLIC_API_KEY") response = chat_with_hyperbolic( messages=[{"role": "user", "content": question}], api_key=api_key ) else: response = predictor.predict( question, api_name="chat" else: # Gemini api_key = os.getenv("GEMINI_API_KEY") response = chat_with_gemini( messages=[{"role": "user", "content": question}], api_key=api_key ) initial_responses.append(f"{model}: {response}")10 unmodified lines random.shuffle(selected_models) # Randomize order each round for model in selected_models: provider, model_name = model.split(": ", 1) registry, api_key = MODEL_REGISTRIES[provider] if not api_key: continue try: discussion_prompt = generate_discussion_prompt(question, discussion_history) if provider == "Anthropic": response = registry.chat( model=model_name, messages=[{"role": "user", "content": discussion_prompt}], api_key=api_key ) api_key = os.getenv("ANTHROPIC_API_KEY") response = chat_with_anthropic( messages=[{"role": "user", "content": discussion_prompt}], api_key=api_key ) elif provider == "SambaNova": api_key = os.getenv("SAMBANOVA_API_KEY") response = chat_with_sambanova( messages=[ {"role": "system", "content": "You are a helpful assistant"}, {"role": "user", "content": discussion_prompt} ], api_key=api_key ) elif provider == "Hyperbolic": # Add Hyperbolic case api_key = os.getenv("HYPERBOLIC_API_KEY") response = chat_with_hyperbolic( messages=[{"role": "user", "content": discussion_prompt}], api_key=api_key ) else: # Gemini api_key = os.getenv("GEMINI_API_KEY") response = chat_with_gemini( messages=[{"role": "user", "content": discussion_prompt}], api_key=api_key ) round_responses.append(f"{model}: {response}") discussion_history.append(f"Round {round_num + 1} - {model}:\n{response}") chat_history.append((f"Round {round_num + 1} - {model}", response)) except Exception as e: chat_history.append((f"Error from {model} in round {round_num + 1}", str(e))) # Final consensus - use the model that's shown most consistency # Final consensus progress(0.9, desc="Building final consensus...") # Use the first model for final consensus instead of two models model = selected_models[0] provider, model_name = model.split(": ", 1) registry, api_key = MODEL_REGISTRIES[provider] try: consensus_prompt = generate_consensus_prompt(question, discussion_history) if provider == "Anthropic": final_consensus = registry.chat( model=model_name, messages=[{"role": "user", "content": consensus_prompt}], api_key=api_key ) api_key = os.getenv("ANTHROPIC_API_KEY") final_consensus = chat_with_anthropic( messages=[{"role": "user", "content": consensus_prompt}], api_key=api_key ) elif provider == "SambaNova": api_key = os.getenv("SAMBANOVA_API_KEY") final_consensus = chat_with_sambanova( messages=[ {"role": "system", "content": "You are a helpful assistant"}, {"role": "user", "content": consensus_prompt} ], api_key=api_key ) elif provider == "Hyperbolic": # Add Hyperbolic case api_key = os.getenv("HYPERBOLIC_API_KEY") final_consensus = chat_with_hyperbolic( messages=[{"role": "user", "content": consensus_prompt}], api_key=api_key ) else: # Gemini api_key = os.getenv("GEMINI_API_KEY") final_consensus = chat_with_gemini( messages=[{"role": "user", "content": consensus_prompt}], api_key=api_key ) except Exception as e: final_consensus = f"Error getting consensus from {model}: {str(e)}" 6 unmodified lines gr.Markdown("# Experimental Multi-Model Consensus Chat") gr.Markdown("""Select multiple models to collaborate on answering your question. The models will discuss with each other and attempt to reach a consensus. Maximum 5 models can be selected at once.""") Maximum 3 models can be selected at once.""") with gr.Row(): with gr.Column(): model_selector = gr.Dropdown( choices=get_all_models(), multiselect=True, label="Select Models (max 5)", info="Choose up to 5 models to participate in the discussion", value=["OpenAI: gpt-4o", "Anthropic: claude-3-5-sonnet-20241022"], # Updated model names max_choices=5 label="Select Models (max 3)", info="Choose up to 3 models to participate in the discussion", value=["SambaNova: Llama-3.2-90B-Vision-Instruct", "Hyperbolic: Qwen/Qwen2.5-Coder-32B-Instruct"], max_choices=3 ) rounds_slider = gr.Slider( minimum=1, maximum=5, value=3, maximum=2, value=1, step=1, label="Discussion Rounds", info="Number of rounds of discussion between models"app.py+4
app_experimental.py+162-88