Spaces:
Running
on
Zero
Running
on
Zero
app.py
CHANGED
|
@@ -1,8 +1,9 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import spaces
|
| 3 |
-
from transformers import pipeline,
|
| 4 |
import torch
|
| 5 |
import logging
|
|
|
|
| 6 |
|
| 7 |
# Configure logging/logger
|
| 8 |
logging.basicConfig(
|
|
@@ -18,6 +19,7 @@ pipelines = {}
|
|
| 18 |
# Predefined list of models to compare (can be expanded)
|
| 19 |
model_options = {
|
| 20 |
"Foundation-Sec-8B": "fdtn-ai/Foundation-Sec-8B",
|
|
|
|
| 21 |
}
|
| 22 |
|
| 23 |
# Initialize models at startup
|
|
@@ -75,11 +77,10 @@ def create_demo():
|
|
| 75 |
|
| 76 |
# Input Section
|
| 77 |
with gr.Row():
|
| 78 |
-
|
| 79 |
value="You are a helpful assistant providing answers for technical and customer support queries.",
|
| 80 |
-
label="
|
| 81 |
)
|
| 82 |
-
user_message = gr.Textbox(label="Your question", placeholder="Type your question here...")
|
| 83 |
|
| 84 |
with gr.Row():
|
| 85 |
max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens")
|
|
@@ -97,38 +98,51 @@ def create_demo():
|
|
| 97 |
|
| 98 |
# Dynamic Response Section
|
| 99 |
response_box1 = gr.Textbox(label="Response from Model 1", interactive=False)
|
| 100 |
-
|
| 101 |
|
| 102 |
# Function to generate responses
|
| 103 |
def generate_responses(
|
| 104 |
-
message,
|
| 105 |
):
|
| 106 |
-
|
| 107 |
-
|
| 108 |
|
| 109 |
if len(selected_models) == 0:
|
| 110 |
-
return "Error: Please select at least one model"
|
| 111 |
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
-
|
| 123 |
-
return response
|
| 124 |
|
| 125 |
# Add a button for generating responses
|
| 126 |
submit_button = gr.Button("Generate Responses")
|
| 127 |
submit_button.click(
|
| 128 |
generate_responses,
|
| 129 |
-
inputs=[
|
| 130 |
-
|
| 131 |
-
outputs=[response_box1]
|
| 132 |
)
|
| 133 |
|
| 134 |
return demo
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import spaces
|
| 3 |
+
from transformers import pipeline, AutoTokenizer
|
| 4 |
import torch
|
| 5 |
import logging
|
| 6 |
+
from concurrent.futures import ThreadPoolExecutor, as_completed
|
| 7 |
|
| 8 |
# Configure logging/logger
|
| 9 |
logging.basicConfig(
|
|
|
|
| 19 |
# Predefined list of models to compare (can be expanded)
|
| 20 |
model_options = {
|
| 21 |
"Foundation-Sec-8B": "fdtn-ai/Foundation-Sec-8B",
|
| 22 |
+
"Llama-3.1-8B": "meta-llama/Llama-3.1-8B",
|
| 23 |
}
|
| 24 |
|
| 25 |
# Initialize models at startup
|
|
|
|
| 77 |
|
| 78 |
# Input Section
|
| 79 |
with gr.Row():
|
| 80 |
+
prompt = gr.Textbox(
|
| 81 |
value="You are a helpful assistant providing answers for technical and customer support queries.",
|
| 82 |
+
label="Prompt"
|
| 83 |
)
|
|
|
|
| 84 |
|
| 85 |
with gr.Row():
|
| 86 |
max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens")
|
|
|
|
| 98 |
|
| 99 |
# Dynamic Response Section
|
| 100 |
response_box1 = gr.Textbox(label="Response from Model 1", interactive=False)
|
| 101 |
+
response_box2 = gr.Textbox(label="Response from Model 2", interactive=False)
|
| 102 |
|
| 103 |
# Function to generate responses
|
| 104 |
def generate_responses(
|
| 105 |
+
message, prompt, max_tokens, temperature, top_p, selected_models
|
| 106 |
):
|
| 107 |
+
if len(selected_models) != 2:
|
| 108 |
+
return "Error: Please select exactly two models to compare.", ""
|
| 109 |
|
| 110 |
if len(selected_models) == 0:
|
| 111 |
+
return "Error: Please select at least one model", ""
|
| 112 |
|
| 113 |
+
# 選択されたモデルの結果を格納する辞書
|
| 114 |
+
responses = {}
|
| 115 |
+
futures_to_model = {} # 各futureとモデルを紐づけるための辞書
|
| 116 |
+
|
| 117 |
+
with ThreadPoolExecutor(max_workers=len(selected_models)) as executor:
|
| 118 |
+
# 各モデルに対してタスクを提出
|
| 119 |
+
futures = []
|
| 120 |
+
for model_name in selected_models:
|
| 121 |
+
model_path = model_options[model_name]
|
| 122 |
+
future = executor.submit(
|
| 123 |
+
generate_text_local,
|
| 124 |
+
model_path,
|
| 125 |
+
prompt,
|
| 126 |
+
max_tokens,
|
| 127 |
+
temperature,
|
| 128 |
+
top_p
|
| 129 |
+
)
|
| 130 |
+
futures.append(future)
|
| 131 |
+
futures_to_model[future] = model_name
|
| 132 |
+
|
| 133 |
+
# 結果の収集
|
| 134 |
+
for future in as_completed(futures):
|
| 135 |
+
model_name = futures_to_model[future]
|
| 136 |
+
responses[model_name] = future.result()
|
| 137 |
|
| 138 |
+
return responses.get(selected_models[0], ""), responses.get(selected_models[1], "")
|
|
|
|
| 139 |
|
| 140 |
# Add a button for generating responses
|
| 141 |
submit_button = gr.Button("Generate Responses")
|
| 142 |
submit_button.click(
|
| 143 |
generate_responses,
|
| 144 |
+
inputs=[prompt, max_tokens, temperature, top_p, selected_models],
|
| 145 |
+
outputs=[response_box1, response_box2], # Link to response boxes
|
|
|
|
| 146 |
)
|
| 147 |
|
| 148 |
return demo
|