as-cle-bert commited on
Commit
146e8fc
·
verified ·
1 Parent(s): b1a3b90

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -58
app.py CHANGED
@@ -1,23 +1,16 @@
1
  import gradio as gr
2
- from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
3
- from pathlib import Path
4
  import pandas as pd
 
 
5
  import spaces
6
 
7
- model_checkpoint = "HuggingFaceTB/SmolLM-1.7B"
8
- model = AutoModelForCausalLM.from_pretrained(model_checkpoint)
9
- tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)
10
 
11
- pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, max_new_tokens=512, repetition_penalty=1.5, temperature=0.5, device="cuda")
12
-
13
-
14
- abs_path = Path(__file__).parent
15
-
16
- df = pd.read_csv(str(abs_path / "models.csv"))
17
- df.to_html("tab.html")
18
 
19
  def refreshfn() -> gr.HTML:
20
- df = pd.read_csv(str(abs_path / "models.csv"))
21
  df.to_html("tab.html")
22
  f = open("tab.html")
23
  content = f.read()
@@ -25,53 +18,75 @@ def refreshfn() -> gr.HTML:
25
  t = gr.HTML(content)
26
  return t
27
 
28
- def chatfn(text):
29
- return text, text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
- with gr.Blocks() as demo:
32
- gr.Markdown("""
33
- # 🥇 Leaderboard Component
34
- """)
35
- with gr.Tabs():
36
- with gr.Tab("Demo"):
37
- f = open("tab.html")
38
- content = f.read()
39
- f.close()
40
- t = gr.HTML(content)
41
- btn = gr.Button("Refresh")
42
- btn.click(fn=refreshfn, inputs=None, outputs=t)
43
- with gr.Tab("Chats"):
44
- import random
45
- import time
46
- with gr.Column():
47
- chatbot = gr.Chatbot()
48
- with gr.Column():
49
- chatbot1 = gr.Chatbot()
50
- msg = gr.Textbox()
51
- clear = gr.ClearButton([msg, chatbot])
52
- @spaces.GPU(duration=200)
53
- def respond(message, chat_history):
54
- response = pipe(message)
55
- bot_message = response[0]["generated_text"]
56
- chat_history.append((message, bot_message))
57
- return "", chat_history
58
 
59
- import concurrent.futures
60
-
61
- def run_functions_simultaneously():
62
- with concurrent.futures.ThreadPoolExecutor() as executor:
63
- # Submit the first function
64
- future1 = executor.submit(msg.submit, respond, [msg, chatbot], [msg, chatbot])
65
-
66
- # Submit the second function
67
- future2 = executor.submit(msg.submit, respond, [msg, chatbot1], [msg, chatbot1])
68
-
69
- # Wait for both futures to complete
70
- concurrent.futures.wait([future1, future2])
 
 
 
 
71
 
72
-
73
- # Call the function to run the tasks simultaneously
74
- run_functions_simultaneously()
75
 
76
  if __name__ == "__main__":
77
- demo.launch()
 
1
  import gradio as gr
 
 
2
  import pandas as pd
3
+ from transformers import pipeline
4
+ from load_models import models_and_tokenizers, models_checkpoints
5
  import spaces
6
 
7
+ choice = {"ModelA": "", "ModelB": ""}
 
 
8
 
9
+ dff = pd.read_csv("models.csv")
10
+ dff.to_html("tab.html")
 
 
 
 
 
11
 
12
  def refreshfn() -> gr.HTML:
13
+ df = pd.read_csv("models.csv")
14
  df.to_html("tab.html")
15
  f = open("tab.html")
16
  content = f.read()
 
18
  t = gr.HTML(content)
19
  return t
20
 
21
+ def rewrite_csv_ordered_by_winning_rate(csv_path):
22
+ # Read the input CSV
23
+ df = pd.read_csv(csv_path)
24
+
25
+ # Sort the DataFrame by WINNING_RATE in descending order
26
+ df_sorted = df.sort_values(by="WINNING_RATE", ascending=False)
27
+
28
+ # Save the sorted DataFrame to a new CSV file
29
+ df_sorted.to_csv(csv_path, index=False)
30
+
31
+ @spaces.GPU(duration=200)
32
+ def run_inference(pipeline, prompt):
33
+ response = pipeline(prompt)
34
+ bot_message = response[0]["generated_text"]
35
+ return bot_message
36
+
37
+ def modelA_button():
38
+ global choice
39
+ df = pd.read_csv("models.csv")
40
+ df.loc[df["MODEL"] == choice["ModelA"], "MATCHES_WON"] += 1
41
+ df.loc[df["MODEL"] == choice["ModelA"], "WINNING_RATE"] = df.loc[df["MODEL"] == choice["ModelA"], "MATCHES_WON"]/df.loc[df["MODEL"] == choice["ModelA"], "MATCHES_PLAYED"]
42
+ df.to_csv("models.csv")
43
+ rewrite_csv_ordered_by_winning_rate("models.csv")
44
+
45
+ def modelB_button():
46
+ global choice
47
+ df = pd.read_csv("models.csv")
48
+ df.loc[df["MODEL"] == choice["ModelB"], "MATCHES_WON"] += 1
49
+ df.loc[df["MODEL"] == choice["ModelB"], "WINNING_RATE"] = df.loc[df["MODEL"] == choice["ModelB"], "MATCHES_WON"]/df.loc[df["MODEL"] == choice["ModelB"], "MATCHES_PLAYED"]
50
+ df.to_csv("models.csv")
51
+ rewrite_csv_ordered_by_winning_rate("models.csv")
52
+
53
+
54
+ def reply(modelA, modelB, prompt):
55
+ global choice
56
+ choice["ModelA"] = modelA
57
+ choice["ModelB"] = modelB
58
+ df = pd.read_csv("models.csv")
59
+ df.loc[df["MODEL"] == modelA, "MATCHES_PLAYED"] += 1
60
+ df.loc[df["MODEL"] == modelB, "MATCHES_PLAYED"] += 1
61
+ df.to_csv("models.csv", index=False)
62
+ pipeA = pipeline("text-generation", model=models_and_tokenizers[modelA][0], tokenizer=models_and_tokenizers[modelA][1], max_new_tokens=512, repetition_penalty=1.5, temperature=0.5, device="cuda")
63
+ pipeB = pipeline("text-generation", model=models_and_tokenizers[modelB][0], tokenizer=models_and_tokenizers[modelB][1], max_new_tokens=512, repetition_penalty=1.5, temperature=0.5, device="cuda")
64
+ responseA = run_inference(pipeA, prompt)
65
+ responseB = run_inference(pipeB, prompt)
66
+ return responseA, responseB
67
 
68
+ modelA_dropdown = gr.Dropdown(models_checkpoints, label="Model A", info="Choose the first model for the battle!")
69
+ modelB_dropdown = gr.Dropdown(models_checkpoints, label="Model B", info="Choose the second model for the battle!")
70
+ prompt_textbox = gr.Textbox(label="Prompt", value="Is pineapple pizza sacrilegious?")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
 
72
+ demo0 = gr.Interface(fn=reply, inputs=[modelA_dropdown, modelB_dropdown, prompt_textbox], outputs=[gr.Markdown(label="Model A response"), gr.Markdown(label="Model B response")])
73
+
74
+ with gr.Blocks() as demo1:
75
+ iface = demo0
76
+ btnA = gr.Button("Vote for Model A!")
77
+ btnB = gr.Button("Vote for Model B!")
78
+ btnA.click(modelA_button, inputs=None, outputs=None)
79
+ btnB.click(modelB_button, inputs=None, outputs=None)
80
+
81
+ with gr.Blocks() as demo2:
82
+ f = open("tab.html")
83
+ content = f.read()
84
+ f.close()
85
+ t = gr.HTML(content)
86
+ btn = gr.Button("Refresh")
87
+ btn.click(fn=refreshfn, inputs=None, outputs=t)
88
 
89
+ demo = gr.TabbedInterface([demo1, demo2], ["Chat Arena", "Leaderboard"])
 
 
90
 
91
  if __name__ == "__main__":
92
+ demo.launch(server_name="0.0.0.0", server_port=7860)