Create main.app
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224import gradio as grimport csvfrom huggingface_hub import InferenceClientimport osfrom datetime import datetimeimport pandas as pd
# Initialize the Hugging Face Inference ClientHF_TOKEN = os.getenv("HUGGINGFACE_TOKEN")client = InferenceClient(token=HF_TOKEN)
# Define the four models to useMODELS = [ "meta-llama/Llama-3.2-3B-Instruct", "mistralai/Mistral-7B-Instruct-v0.3", "google/gemma-2-2b-it", "Qwen/Qwen2.5-7B-Instruct"]
def get_llm_response(model_name, prompt, max_tokens=500, temperature=0.7): """ Get response from a specific LLM model. Each call is independent with no conversation history. """ try: # Create a fresh client for each request to ensure no state persistence fresh_client = InferenceClient(token=HF_TOKEN) # Only send the current prompt - no conversation history response = fresh_client.chat_completion( model=model_name, messages=[{"role": "user", "content": prompt}], max_tokens=max_tokens, temperature=temperature ) return response.choices[0].message.content except Exception as e: return f"Error: {str(e)}"
def collect_responses(prompt_text, max_tokens=500, temperature=0.7): """ Collect responses from all four models for a given prompt and return as a dataframe and CSV file. Each model gets a fresh, independent query with no history. """ results = [] status_updates = [] for model in MODELS: status_updates.append(f"β³ Querying {model}...") yield "\n".join(status_updates), None, None response = get_llm_response(model, prompt_text, max_tokens, temperature) result = { 'timestamp': datetime.now().isoformat(), 'prompt': prompt_text, 'model': model, 'response': response } results.append(result) status_updates[-1] = f"β Completed {model}" yield "\n".join(status_updates), None, None # Create DataFrame df = pd.DataFrame(results) # Save to CSV csv_filename = f"llm_responses_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv" df.to_csv(csv_filename, index=False) status_updates.append(f"\nβ
All responses collected! Saved to {csv_filename}") yield "\n".join(status_updates), df, csv_filename
def batch_collect_responses(prompts_text, max_tokens=500, temperature=0.7): """ Collect responses for multiple prompts (one per line). Each prompt is processed independently with no conversation history. """ prompts = [p.strip() for p in prompts_text.split('\n') if p.strip()] if not prompts: return "β Please enter at least one prompt", None, None all_results = [] status_updates = [] for i, prompt in enumerate(prompts, 1): status_updates.append(f"\nπ Processing prompt {i}/{len(prompts)}: {prompt[:50]}...") yield "\n".join(status_updates), None, None for model in MODELS: status_updates.append(f" β³ Querying {model}...") yield "\n".join(status_updates), None, None response = get_llm_response(model, prompt, max_tokens, temperature) result = { 'timestamp': datetime.now().isoformat(), 'prompt': prompt, 'model': model, 'response': response } all_results.append(result) status_updates[-1] = f" β Completed {model}" yield "\n".join(status_updates), None, None # Create DataFrame df = pd.DataFrame(all_results) # Save to CSV csv_filename = f"llm_responses_batch_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv" df.to_csv(csv_filename, index=False) status_updates.append(f"\nβ
All responses collected! Saved to {csv_filename}") yield "\n".join(status_updates), df, csv_filename
# Create Gradio interfacewith gr.Blocks(title="Multi-LLM Response Collector") as demo: gr.Markdown(""" # π€ Multi-LLM Response Collector Collect and compare **one-shot** responses from four different LLMs: - Meta Llama 3.2 3B - Mistral 7B - Google Gemma 2 2B - Qwen 2.5 7B **Important:** Each query is independent with no conversation history. Every prompt gets a fresh response with zero context from previous queries. Responses are saved to a CSV file for easy analysis. """) with gr.Tab("Single Prompt"): with gr.Row(): with gr.Column(): prompt_input = gr.Textbox( label="Enter your prompt", placeholder="e.g., What is artificial intelligence?", lines=3 ) max_tokens_single = gr.Slider( minimum=100, maximum=1000, value=500, step=50, label="Max Tokens" ) temperature_single = gr.Slider( minimum=0.0, maximum=2.0, value=0.7, step=0.1, label="Temperature (creativity)" ) submit_btn = gr.Button("Collect Responses", variant="primary") status_output = gr.Textbox(label="Status", lines=6) with gr.Row(): df_output = gr.Dataframe(label="Responses") csv_output = gr.File(label="Download CSV") submit_btn.click( fn=collect_responses, inputs=[prompt_input, max_tokens_single, temperature_single], outputs=[status_output, df_output, csv_output] ) with gr.Tab("Batch Prompts"): with gr.Row(): with gr.Column(): batch_input = gr.Textbox( label="Enter prompts (one per line)", placeholder="What is AI?\nExplain machine learning.\nWhat is deep learning?", lines=5 ) max_tokens_batch = gr.Slider( minimum=100, maximum=1000, value=500, step=50, label="Max Tokens" ) temperature_batch = gr.Slider( minimum=0.0, maximum=2.0, value=0.7, step=0.1, label="Temperature (creativity)" ) batch_btn = gr.Button("Collect Batch Responses", variant="primary") batch_status = gr.Textbox(label="Status", lines=10) with gr.Row(): batch_df = gr.Dataframe(label="All Responses") batch_csv = gr.File(label="Download CSV") batch_btn.click( fn=batch_collect_responses, inputs=[batch_input, max_tokens_batch, temperature_batch], outputs=[batch_status, batch_df, batch_csv] ) gr.Markdown(""" --- ### π CSV Format The output CSV contains: - `timestamp`: When the response was generated - `prompt`: The input prompt - `model`: Which model generated the response - `response`: The model's response """)
if __name__ == "__main__": demo.launch()main.appAdded+224