Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,14 @@
|
|
1 |
import os
|
2 |
from threading import Thread
|
3 |
from typing import Iterator, List, Tuple
|
|
|
4 |
|
5 |
import gradio as gr
|
6 |
import spaces
|
7 |
import torch
|
8 |
import transformers
|
9 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
|
|
10 |
|
11 |
DESCRIPTION = """\
|
12 |
# Zero GPU Model Comparison Arena
|
@@ -28,6 +30,25 @@ MODEL_OPTIONS = [
|
|
28 |
models = {}
|
29 |
tokenizers = {}
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
for model_id in MODEL_OPTIONS:
|
32 |
tokenizers[model_id] = AutoTokenizer.from_pretrained(model_id)
|
33 |
models[model_id] = AutoModelForCausalLM.from_pretrained(
|
@@ -36,6 +57,22 @@ for model_id in MODEL_OPTIONS:
|
|
36 |
load_in_8bit=True,
|
37 |
)
|
38 |
models[model_id].eval()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
@spaces.GPU(duration=90)
|
41 |
def generate(
|
@@ -101,11 +138,11 @@ def compare_models(
|
|
101 |
chat_history1.append((message, output1))
|
102 |
chat_history2.append((message, output2))
|
103 |
|
104 |
-
|
105 |
|
106 |
return chat_history1, chat_history2, chat_history1, chat_history2
|
107 |
|
108 |
-
def
|
109 |
log_data = {
|
110 |
"question": question,
|
111 |
"model1": {"name": model1_name, "answer": answer1},
|
@@ -113,12 +150,20 @@ def log_results(model1_name: str, model2_name: str, question: str, answer1: str,
|
|
113 |
"winner": winner
|
114 |
}
|
115 |
|
116 |
-
#
|
117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
|
119 |
def vote_better(model1_name, model2_name, question, answer1, answer2, choice):
|
120 |
winner = model1_name if choice == "Model 1" else model2_name
|
121 |
-
|
122 |
return f"You voted that {winner} performs better. This has been logged."
|
123 |
|
124 |
with gr.Blocks(css="style.css") as demo:
|
@@ -166,4 +211,9 @@ with gr.Blocks(css="style.css") as demo:
|
|
166 |
)
|
167 |
|
168 |
if __name__ == "__main__":
|
169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
from threading import Thread
|
3 |
from typing import Iterator, List, Tuple
|
4 |
+
import json
|
5 |
|
6 |
import gradio as gr
|
7 |
import spaces
|
8 |
import torch
|
9 |
import transformers
|
10 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
11 |
+
from flask import Flask, request, jsonify
|
12 |
|
13 |
DESCRIPTION = """\
|
14 |
# Zero GPU Model Comparison Arena
|
|
|
30 |
models = {}
|
31 |
tokenizers = {}
|
32 |
|
33 |
+
# Custom chat templates
|
34 |
+
MISTRAL_TEMPLATE = """<s>[INST] {instruction} [/INST]
|
35 |
+
{response}
|
36 |
+
</s>
|
37 |
+
<s>[INST] {instruction} [/INST]
|
38 |
+
"""
|
39 |
+
|
40 |
+
LLAMA_TEMPLATE = """<s>[INST] <<SYS>>
|
41 |
+
You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.
|
42 |
+
|
43 |
+
If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.
|
44 |
+
<</SYS>>
|
45 |
+
|
46 |
+
{instruction} [/INST]
|
47 |
+
{response}
|
48 |
+
</s>
|
49 |
+
<s>[INST] {instruction} [/INST]
|
50 |
+
"""
|
51 |
+
|
52 |
for model_id in MODEL_OPTIONS:
|
53 |
tokenizers[model_id] = AutoTokenizer.from_pretrained(model_id)
|
54 |
models[model_id] = AutoModelForCausalLM.from_pretrained(
|
|
|
57 |
load_in_8bit=True,
|
58 |
)
|
59 |
models[model_id].eval()
|
60 |
+
|
61 |
+
# Set custom chat templates
|
62 |
+
if "Navarna" in model_id:
|
63 |
+
tokenizers[model_id].chat_template = MISTRAL_TEMPLATE
|
64 |
+
elif "OpenHathi" in model_id:
|
65 |
+
tokenizers[model_id].chat_template = LLAMA_TEMPLATE
|
66 |
+
|
67 |
+
# Initialize Flask app
|
68 |
+
app = Flask(__name__)
|
69 |
+
|
70 |
+
@app.route('/log', methods=['POST'])
|
71 |
+
def log_results():
|
72 |
+
data = request.json
|
73 |
+
# Here you can implement any additional processing or storage logic
|
74 |
+
print("Logged:", json.dumps(data, indent=2))
|
75 |
+
return jsonify({"status": "success"}), 200
|
76 |
|
77 |
@spaces.GPU(duration=90)
|
78 |
def generate(
|
|
|
138 |
chat_history1.append((message, output1))
|
139 |
chat_history2.append((message, output2))
|
140 |
|
141 |
+
log_comparison(model1_name, model2_name, message, output1, output2)
|
142 |
|
143 |
return chat_history1, chat_history2, chat_history1, chat_history2
|
144 |
|
145 |
+
def log_comparison(model1_name: str, model2_name: str, question: str, answer1: str, answer2: str, winner: str = None):
|
146 |
log_data = {
|
147 |
"question": question,
|
148 |
"model1": {"name": model1_name, "answer": answer1},
|
|
|
150 |
"winner": winner
|
151 |
}
|
152 |
|
153 |
+
# Send log data to Flask server
|
154 |
+
import requests
|
155 |
+
try:
|
156 |
+
response = requests.post('http://144.24.151.32:5000/log', json=log_data)
|
157 |
+
if response.status_code == 200:
|
158 |
+
print("Successfully logged to server")
|
159 |
+
else:
|
160 |
+
print(f"Failed to log to server. Status code: {response.status_code}")
|
161 |
+
except requests.RequestException as e:
|
162 |
+
print(f"Error sending log to server: {e}")
|
163 |
|
164 |
def vote_better(model1_name, model2_name, question, answer1, answer2, choice):
|
165 |
winner = model1_name if choice == "Model 1" else model2_name
|
166 |
+
log_comparison(model1_name, model2_name, question, answer1, answer2, winner)
|
167 |
return f"You voted that {winner} performs better. This has been logged."
|
168 |
|
169 |
with gr.Blocks(css="style.css") as demo:
|
|
|
211 |
)
|
212 |
|
213 |
if __name__ == "__main__":
|
214 |
+
# Start Flask server in a separate thread
|
215 |
+
flask_thread = Thread(target=app.run, kwargs={"host": "0.0.0.0", "port": 5000})
|
216 |
+
flask_thread.start()
|
217 |
+
|
218 |
+
# Start Gradio app
|
219 |
+
demo.queue(max_size=10).launch()
|