Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -35,4 +35,48 @@ with gr.Blocks() as demo:
|
|
| 35 |
|
| 36 |
run_button.click(fn=evaluate_prompt, inputs=[model_name, prompt_text], outputs=output)
|
| 37 |
|
| 38 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
run_button.click(fn=evaluate_prompt, inputs=[model_name, prompt_text], outputs=output)
|
| 37 |
|
| 38 |
+
demo.launch()
|
| 39 |
+
import pandas as pd
|
| 40 |
+
|
| 41 |
+
def batch_evaluate(file, model_name):
|
| 42 |
+
df = pd.read_csv(file.name) # CSV must have a column called 'prompt'
|
| 43 |
+
results = []
|
| 44 |
+
for idx, row in df.iterrows():
|
| 45 |
+
prompt_text = row['prompt']
|
| 46 |
+
response = openai.ChatCompletion.create(
|
| 47 |
+
model=model_name,
|
| 48 |
+
messages=[{"role":"user","content":prompt_text}],
|
| 49 |
+
temperature=0.5
|
| 50 |
+
)
|
| 51 |
+
results.append({
|
| 52 |
+
'prompt': prompt_text,
|
| 53 |
+
'model': model_name,
|
| 54 |
+
'response': response['choices'][0]['message']['content']
|
| 55 |
+
})
|
| 56 |
+
return pd.DataFrame(results)
|
| 57 |
+
|
| 58 |
+
with gr.Blocks() as demo:
|
| 59 |
+
csv_input = gr.File(label="Upload CSV with 'prompt' column")
|
| 60 |
+
model_input = gr.Textbox(label="Model Name")
|
| 61 |
+
run_button = gr.Button("Run Batch Eval")
|
| 62 |
+
output_table = gr.Dataframe(headers=["prompt", "model", "response"])
|
| 63 |
+
|
| 64 |
+
run_button.click(batch_evaluate, inputs=[csv_input, model_input], outputs=output_table)
|
| 65 |
+
|
| 66 |
+
demo.launch()
|
| 67 |
+
import io
|
| 68 |
+
|
| 69 |
+
def results_to_csv(df):
|
| 70 |
+
buffer = io.StringIO()
|
| 71 |
+
df.to_csv(buffer, index=False)
|
| 72 |
+
buffer.seek(0)
|
| 73 |
+
return buffer
|
| 74 |
+
|
| 75 |
+
download_btn = gr.Button("Download CSV")
|
| 76 |
+
download_file = gr.File()
|
| 77 |
+
|
| 78 |
+
download_btn.click(results_to_csv, inputs=output_table, outputs=download_file)
|
| 79 |
+
def metrics(df):
|
| 80 |
+
df['length'] = df['response'].apply(len)
|
| 81 |
+
summary = df.groupby('model').agg(avg_length=('length','mean')).reset_index()
|
| 82 |
+
return summary
|