Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
| 3 |
|
| 4 |
MODELS = {
|
| 5 |
"Zephyr 7B Beta": "HuggingFaceH4/zephyr-7b-beta",
|
|
@@ -9,8 +10,19 @@ MODELS = {
|
|
| 9 |
"Cohere Command R+": "CohereForAI/c4ai-command-r-plus",
|
| 10 |
}
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
def get_client(model_name):
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
def respond(
|
| 16 |
message,
|
|
@@ -21,7 +33,11 @@ def respond(
|
|
| 21 |
top_p,
|
| 22 |
system_message,
|
| 23 |
):
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
messages = [{"role": "system", "content": system_message}]
|
| 26 |
|
| 27 |
for human, assistant in chat_history:
|
|
@@ -43,6 +59,9 @@ def respond(
|
|
| 43 |
response += token
|
| 44 |
yield response
|
| 45 |
|
|
|
|
|
|
|
|
|
|
| 46 |
with gr.Blocks() as demo:
|
| 47 |
gr.Markdown("# Advanced AI Chatbot")
|
| 48 |
gr.Markdown("Chat with different language models and customize your experience!")
|
|
@@ -66,10 +85,13 @@ with gr.Blocks() as demo:
|
|
| 66 |
with gr.Column(scale=2):
|
| 67 |
chatbot = gr.Chatbot()
|
| 68 |
msg = gr.Textbox(label="Your message")
|
| 69 |
-
|
|
|
|
|
|
|
| 70 |
|
| 71 |
msg.submit(respond, [msg, chatbot, model_name, max_tokens, temperature, top_p, system_message], chatbot)
|
| 72 |
-
|
|
|
|
| 73 |
|
| 74 |
if __name__ == "__main__":
|
| 75 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
MODELS = {
|
| 6 |
"Zephyr 7B Beta": "HuggingFaceH4/zephyr-7b-beta",
|
|
|
|
| 10 |
"Cohere Command R+": "CohereForAI/c4ai-command-r-plus",
|
| 11 |
}
|
| 12 |
|
| 13 |
+
MODELS_REQUIRING_TOKEN = [
|
| 14 |
+
"meta-llama/Meta-Llama-3.1-8B-Instruct",
|
| 15 |
+
"CohereForAI/c4ai-command-r-plus"
|
| 16 |
+
]
|
| 17 |
+
|
| 18 |
def get_client(model_name):
|
| 19 |
+
model_id = MODELS[model_name]
|
| 20 |
+
if model_id in MODELS_REQUIRING_TOKEN:
|
| 21 |
+
hf_token = os.getenv("HF_TOKEN")
|
| 22 |
+
if not hf_token:
|
| 23 |
+
raise ValueError(f"HF_TOKEN environment variable is required for {model_name}")
|
| 24 |
+
return InferenceClient(model_id, token=hf_token)
|
| 25 |
+
return InferenceClient(model_id)
|
| 26 |
|
| 27 |
def respond(
|
| 28 |
message,
|
|
|
|
| 33 |
top_p,
|
| 34 |
system_message,
|
| 35 |
):
|
| 36 |
+
try:
|
| 37 |
+
client = get_client(model_name)
|
| 38 |
+
except ValueError as e:
|
| 39 |
+
return str(e)
|
| 40 |
+
|
| 41 |
messages = [{"role": "system", "content": system_message}]
|
| 42 |
|
| 43 |
for human, assistant in chat_history:
|
|
|
|
| 59 |
response += token
|
| 60 |
yield response
|
| 61 |
|
| 62 |
+
def clear_conversation():
|
| 63 |
+
return None
|
| 64 |
+
|
| 65 |
with gr.Blocks() as demo:
|
| 66 |
gr.Markdown("# Advanced AI Chatbot")
|
| 67 |
gr.Markdown("Chat with different language models and customize your experience!")
|
|
|
|
| 85 |
with gr.Column(scale=2):
|
| 86 |
chatbot = gr.Chatbot()
|
| 87 |
msg = gr.Textbox(label="Your message")
|
| 88 |
+
with gr.Row():
|
| 89 |
+
submit_button = gr.Button("Submit")
|
| 90 |
+
clear_button = gr.Button("Clear")
|
| 91 |
|
| 92 |
msg.submit(respond, [msg, chatbot, model_name, max_tokens, temperature, top_p, system_message], chatbot)
|
| 93 |
+
submit_button.click(respond, [msg, chatbot, model_name, max_tokens, temperature, top_p, system_message], chatbot)
|
| 94 |
+
clear_button.click(clear_conversation, outputs=chatbot, queue=False)
|
| 95 |
|
| 96 |
if __name__ == "__main__":
|
| 97 |
demo.launch()
|