Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,62 +1,85 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
-
import
|
4 |
-
import
|
5 |
-
|
6 |
-
#
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
)
|
13 |
-
|
14 |
-
#
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
prompt=prompt,
|
29 |
-
max_tokens=150
|
30 |
-
)
|
31 |
-
return response['choices'][0]['text'].strip()
|
32 |
|
33 |
-
#
|
34 |
-
def compare_models(prompt, models):
|
35 |
responses = {}
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
return responses
|
41 |
|
42 |
-
# Gradio
|
43 |
-
def
|
44 |
-
|
45 |
-
gr.Markdown("# AI Model Comparison Tool
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
if __name__ == "__main__":
|
62 |
-
|
|
|
|
1 |
+
import os
|
2 |
import gradio as gr
|
3 |
+
from huggingface_hub import login
|
4 |
+
from huggingface_hub import InferenceClient
|
5 |
+
import spaces
|
6 |
+
|
7 |
+
# Authenticate with Hugging Face API
|
8 |
+
api_key = os.getenv("LLAMA")
|
9 |
+
login(api_key)
|
10 |
+
|
11 |
+
# Initialize clients for different models
|
12 |
+
llama_client = InferenceClient("meta-llama/Llama-3.1-70B-Instruct")
|
13 |
+
gpt_client = InferenceClient("openai/gpt-4") # Example: Replace with your OpenAI GPT model
|
14 |
+
|
15 |
+
# Define the response function
|
16 |
+
@spaces.GPU
|
17 |
+
def respond(
|
18 |
+
message,
|
19 |
+
history: list[dict],
|
20 |
+
system_message,
|
21 |
+
max_tokens,
|
22 |
+
temperature,
|
23 |
+
top_p,
|
24 |
+
selected_models,
|
25 |
+
):
|
26 |
+
# Prepare input messages
|
27 |
+
messages = [{"role": "system", "content": system_message}] + history
|
28 |
+
messages.append({"role": "user", "content": message})
|
|
|
|
|
|
|
|
|
29 |
|
30 |
+
# Collect responses from selected models
|
|
|
31 |
responses = {}
|
32 |
+
|
33 |
+
if "Llama" in selected_models:
|
34 |
+
llama_response = ""
|
35 |
+
for token in llama_client.chat_completion(
|
36 |
+
messages, max_tokens=max_tokens, stream=True, temperature=temperature, top_p=top_p
|
37 |
+
):
|
38 |
+
delta = token.choices[0].delta.content
|
39 |
+
llama_response += delta
|
40 |
+
responses["Llama"] = llama_response
|
41 |
+
|
42 |
+
if "GPT" in selected_models:
|
43 |
+
gpt_response = ""
|
44 |
+
for token in gpt_client.chat_completion(
|
45 |
+
messages, max_tokens=max_tokens, stream=True, temperature=temperature, top_p=top_p
|
46 |
+
):
|
47 |
+
delta = token.choices[0].delta.content
|
48 |
+
gpt_response += delta
|
49 |
+
responses["GPT"] = gpt_response
|
50 |
+
|
51 |
return responses
|
52 |
|
53 |
+
# Build the Gradio app
|
54 |
+
def create_demo():
|
55 |
+
return gr.Blocks().add(
|
56 |
+
gr.Markdown("# AI Model Comparison Tool 🌟"),
|
57 |
+
gr.ChatInterface(
|
58 |
+
respond,
|
59 |
+
type="messages",
|
60 |
+
additional_inputs=[
|
61 |
+
gr.Textbox(
|
62 |
+
value="You are a helpful assistant providing answers for technical and customer support queries.",
|
63 |
+
label="System message"
|
64 |
+
),
|
65 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
66 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
67 |
+
gr.Slider(
|
68 |
+
minimum=0.1,
|
69 |
+
maximum=1.0,
|
70 |
+
value=0.95,
|
71 |
+
step=0.05,
|
72 |
+
label="Top-p (nucleus sampling)"
|
73 |
+
),
|
74 |
+
gr.CheckboxGroup(
|
75 |
+
["Llama", "GPT"],
|
76 |
+
label="Select models to compare",
|
77 |
+
value=["Llama"]
|
78 |
+
),
|
79 |
+
],
|
80 |
+
),
|
81 |
+
)
|
82 |
|
83 |
if __name__ == "__main__":
|
84 |
+
demo = create_demo()
|
85 |
+
demo.launch()
|