kambris commited on
Commit
9bc63b4
Β·
verified Β·
1 Parent(s): fdfccc4

Create main.app

Browse files
Files changed (1) hide show
  1. main.app +224 -0
main.app ADDED
@@ -0,0 +1,224 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import csv
3
+ from huggingface_hub import InferenceClient
4
+ import os
5
+ from datetime import datetime
6
+ import pandas as pd
7
+
8
+ # Initialize the Hugging Face Inference Client
9
+ HF_TOKEN = os.getenv("HUGGINGFACE_TOKEN")
10
+ client = InferenceClient(token=HF_TOKEN)
11
+
12
+ # Define the four models to use
13
+ MODELS = [
14
+ "meta-llama/Llama-3.2-3B-Instruct",
15
+ "mistralai/Mistral-7B-Instruct-v0.3",
16
+ "google/gemma-2-2b-it",
17
+ "Qwen/Qwen2.5-7B-Instruct"
18
+ ]
19
+
20
+ def get_llm_response(model_name, prompt, max_tokens=500, temperature=0.7):
21
+ """
22
+ Get response from a specific LLM model.
23
+ Each call is independent with no conversation history.
24
+ """
25
+ try:
26
+ # Create a fresh client for each request to ensure no state persistence
27
+ fresh_client = InferenceClient(token=HF_TOKEN)
28
+
29
+ # Only send the current prompt - no conversation history
30
+ response = fresh_client.chat_completion(
31
+ model=model_name,
32
+ messages=[{"role": "user", "content": prompt}],
33
+ max_tokens=max_tokens,
34
+ temperature=temperature
35
+ )
36
+ return response.choices[0].message.content
37
+ except Exception as e:
38
+ return f"Error: {str(e)}"
39
+
40
+ def collect_responses(prompt_text, max_tokens=500, temperature=0.7):
41
+ """
42
+ Collect responses from all four models for a given prompt
43
+ and return as a dataframe and CSV file.
44
+ Each model gets a fresh, independent query with no history.
45
+ """
46
+ results = []
47
+ status_updates = []
48
+
49
+ for model in MODELS:
50
+ status_updates.append(f"⏳ Querying {model}...")
51
+ yield "\n".join(status_updates), None, None
52
+
53
+ response = get_llm_response(model, prompt_text, max_tokens, temperature)
54
+
55
+ result = {
56
+ 'timestamp': datetime.now().isoformat(),
57
+ 'prompt': prompt_text,
58
+ 'model': model,
59
+ 'response': response
60
+ }
61
+ results.append(result)
62
+
63
+ status_updates[-1] = f"βœ“ Completed {model}"
64
+ yield "\n".join(status_updates), None, None
65
+
66
+ # Create DataFrame
67
+ df = pd.DataFrame(results)
68
+
69
+ # Save to CSV
70
+ csv_filename = f"llm_responses_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv"
71
+ df.to_csv(csv_filename, index=False)
72
+
73
+ status_updates.append(f"\nβœ… All responses collected! Saved to {csv_filename}")
74
+
75
+ yield "\n".join(status_updates), df, csv_filename
76
+
77
+ def batch_collect_responses(prompts_text, max_tokens=500, temperature=0.7):
78
+ """
79
+ Collect responses for multiple prompts (one per line).
80
+ Each prompt is processed independently with no conversation history.
81
+ """
82
+ prompts = [p.strip() for p in prompts_text.split('\n') if p.strip()]
83
+
84
+ if not prompts:
85
+ return "❌ Please enter at least one prompt", None, None
86
+
87
+ all_results = []
88
+ status_updates = []
89
+
90
+ for i, prompt in enumerate(prompts, 1):
91
+ status_updates.append(f"\nπŸ“ Processing prompt {i}/{len(prompts)}: {prompt[:50]}...")
92
+ yield "\n".join(status_updates), None, None
93
+
94
+ for model in MODELS:
95
+ status_updates.append(f" ⏳ Querying {model}...")
96
+ yield "\n".join(status_updates), None, None
97
+
98
+ response = get_llm_response(model, prompt, max_tokens, temperature)
99
+
100
+ result = {
101
+ 'timestamp': datetime.now().isoformat(),
102
+ 'prompt': prompt,
103
+ 'model': model,
104
+ 'response': response
105
+ }
106
+ all_results.append(result)
107
+
108
+ status_updates[-1] = f" βœ“ Completed {model}"
109
+ yield "\n".join(status_updates), None, None
110
+
111
+ # Create DataFrame
112
+ df = pd.DataFrame(all_results)
113
+
114
+ # Save to CSV
115
+ csv_filename = f"llm_responses_batch_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv"
116
+ df.to_csv(csv_filename, index=False)
117
+
118
+ status_updates.append(f"\nβœ… All responses collected! Saved to {csv_filename}")
119
+
120
+ yield "\n".join(status_updates), df, csv_filename
121
+
122
+ # Create Gradio interface
123
+ with gr.Blocks(title="Multi-LLM Response Collector") as demo:
124
+ gr.Markdown("""
125
+ # πŸ€– Multi-LLM Response Collector
126
+
127
+ Collect and compare **one-shot** responses from four different LLMs:
128
+ - Meta Llama 3.2 3B
129
+ - Mistral 7B
130
+ - Google Gemma 2 2B
131
+ - Qwen 2.5 7B
132
+
133
+ **Important:** Each query is independent with no conversation history.
134
+ Every prompt gets a fresh response with zero context from previous queries.
135
+
136
+ Responses are saved to a CSV file for easy analysis.
137
+ """)
138
+
139
+ with gr.Tab("Single Prompt"):
140
+ with gr.Row():
141
+ with gr.Column():
142
+ prompt_input = gr.Textbox(
143
+ label="Enter your prompt",
144
+ placeholder="e.g., What is artificial intelligence?",
145
+ lines=3
146
+ )
147
+ max_tokens_single = gr.Slider(
148
+ minimum=100,
149
+ maximum=1000,
150
+ value=500,
151
+ step=50,
152
+ label="Max Tokens"
153
+ )
154
+ temperature_single = gr.Slider(
155
+ minimum=0.0,
156
+ maximum=2.0,
157
+ value=0.7,
158
+ step=0.1,
159
+ label="Temperature (creativity)"
160
+ )
161
+ submit_btn = gr.Button("Collect Responses", variant="primary")
162
+
163
+ status_output = gr.Textbox(label="Status", lines=6)
164
+
165
+ with gr.Row():
166
+ df_output = gr.Dataframe(label="Responses")
167
+
168
+ csv_output = gr.File(label="Download CSV")
169
+
170
+ submit_btn.click(
171
+ fn=collect_responses,
172
+ inputs=[prompt_input, max_tokens_single, temperature_single],
173
+ outputs=[status_output, df_output, csv_output]
174
+ )
175
+
176
+ with gr.Tab("Batch Prompts"):
177
+ with gr.Row():
178
+ with gr.Column():
179
+ batch_input = gr.Textbox(
180
+ label="Enter prompts (one per line)",
181
+ placeholder="What is AI?\nExplain machine learning.\nWhat is deep learning?",
182
+ lines=5
183
+ )
184
+ max_tokens_batch = gr.Slider(
185
+ minimum=100,
186
+ maximum=1000,
187
+ value=500,
188
+ step=50,
189
+ label="Max Tokens"
190
+ )
191
+ temperature_batch = gr.Slider(
192
+ minimum=0.0,
193
+ maximum=2.0,
194
+ value=0.7,
195
+ step=0.1,
196
+ label="Temperature (creativity)"
197
+ )
198
+ batch_btn = gr.Button("Collect Batch Responses", variant="primary")
199
+
200
+ batch_status = gr.Textbox(label="Status", lines=10)
201
+
202
+ with gr.Row():
203
+ batch_df = gr.Dataframe(label="All Responses")
204
+
205
+ batch_csv = gr.File(label="Download CSV")
206
+
207
+ batch_btn.click(
208
+ fn=batch_collect_responses,
209
+ inputs=[batch_input, max_tokens_batch, temperature_batch],
210
+ outputs=[batch_status, batch_df, batch_csv]
211
+ )
212
+
213
+ gr.Markdown("""
214
+ ---
215
+ ### πŸ“Š CSV Format
216
+ The output CSV contains:
217
+ - `timestamp`: When the response was generated
218
+ - `prompt`: The input prompt
219
+ - `model`: Which model generated the response
220
+ - `response`: The model's response
221
+ """)
222
+
223
+ if __name__ == "__main__":
224
+ demo.launch()