Update app.py
Browse files
app.py
CHANGED
|
@@ -1,88 +1,115 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def respond(
|
| 11 |
-
message,
|
| 12 |
-
history: list[tuple[str, str]],
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
top_p,
|
| 17 |
):
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
for val in history:
|
| 21 |
if val[0]:
|
| 22 |
messages.append({"role": "user", "content": val[0]})
|
| 23 |
if val[1]:
|
| 24 |
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
-
|
| 26 |
messages.append({"role": "user", "content": message})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
],
|
| 66 |
-
)
|
| 67 |
-
|
| 68 |
-
# Combine both interfaces into a single app
|
| 69 |
-
demo = gr.ChatInterface(
|
| 70 |
-
respond,
|
| 71 |
-
additional_inputs=[
|
| 72 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 73 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 74 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 75 |
-
gr.Slider(
|
| 76 |
-
minimum=0.1,
|
| 77 |
-
maximum=1.0,
|
| 78 |
-
value=0.95,
|
| 79 |
-
step=0.05,
|
| 80 |
-
label="Top-p (nucleus sampling)",
|
| 81 |
-
),
|
| 82 |
-
],
|
| 83 |
-
)
|
| 84 |
|
| 85 |
if __name__ == "__main__":
|
| 86 |
-
|
| 87 |
-
demo.launch(share=True, server_name="0.0.0.0", server_port=7860)
|
| 88 |
-
api_demo.launch(share=True, server_name="0.0.0.0", server_port=7861)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient, HfApi
|
| 3 |
+
from huggingface_hub.utils import HfHubHTTPError
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
+
def check_and_enable_api(model_id):
|
| 7 |
+
try:
|
| 8 |
+
api = HfApi()
|
| 9 |
+
model_info = api.model_info(model_id)
|
| 10 |
+
if not model_info.api:
|
| 11 |
+
api.toggle_model_api(model_id, True)
|
| 12 |
+
return "API was disabled. Now enabled for the model."
|
| 13 |
+
return "API already enabled for the model."
|
| 14 |
+
except Exception as e:
|
| 15 |
+
return f"Error checking API status: {str(e)}"
|
| 16 |
|
| 17 |
+
def get_api_status():
|
| 18 |
+
token = os.getenv('HF_TOKEN')
|
| 19 |
+
model_id = "HuggingFaceH4/zephyr-7b-beta"
|
| 20 |
+
|
| 21 |
+
if not token:
|
| 22 |
+
return "⚠️ No API token found. Please set HF_TOKEN environment variable."
|
| 23 |
+
|
| 24 |
+
try:
|
| 25 |
+
status = check_and_enable_api(model_id)
|
| 26 |
+
client = InferenceClient(model_id, token=token)
|
| 27 |
+
return f"✅ Connected to {model_id} | {status}"
|
| 28 |
+
except HfHubHTTPError as e:
|
| 29 |
+
return f"❌ API Error: {str(e)}"
|
| 30 |
+
except Exception as e:
|
| 31 |
+
return f"❌ Error: {str(e)}"
|
| 32 |
|
| 33 |
def respond(
|
| 34 |
+
message,
|
| 35 |
+
history: list[tuple[str, str]],
|
| 36 |
+
system_message,
|
| 37 |
+
max_tokens,
|
| 38 |
+
temperature,
|
| 39 |
top_p,
|
| 40 |
):
|
| 41 |
+
token = os.getenv('HF_TOKEN')
|
| 42 |
+
if not token:
|
| 43 |
+
yield "Error: Please set your HuggingFace API token in the HF_TOKEN environment variable."
|
| 44 |
+
return
|
| 45 |
|
| 46 |
+
client = InferenceClient(
|
| 47 |
+
"HuggingFaceH4/zephyr-7b-beta",
|
| 48 |
+
token=token
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
messages = [{"role": "system", "content": system_message}]
|
| 52 |
+
|
| 53 |
for val in history:
|
| 54 |
if val[0]:
|
| 55 |
messages.append({"role": "user", "content": val[0]})
|
| 56 |
if val[1]:
|
| 57 |
messages.append({"role": "assistant", "content": val[1]})
|
| 58 |
+
|
| 59 |
messages.append({"role": "user", "content": message})
|
| 60 |
+
|
| 61 |
+
try:
|
| 62 |
+
response = ""
|
| 63 |
+
for message in client.chat_completion(
|
| 64 |
+
messages,
|
| 65 |
+
max_tokens=max_tokens,
|
| 66 |
+
stream=True,
|
| 67 |
+
temperature=temperature,
|
| 68 |
+
top_p=top_p,
|
| 69 |
+
):
|
| 70 |
+
token = message.choices[0].delta.content
|
| 71 |
+
response += token
|
| 72 |
+
yield response
|
| 73 |
+
except Exception as e:
|
| 74 |
+
yield f"Error during chat completion: {str(e)}"
|
| 75 |
|
| 76 |
+
with gr.Blocks() as demo:
|
| 77 |
+
chatbot = gr.ChatInterface(
|
| 78 |
+
respond,
|
| 79 |
+
additional_inputs=[
|
| 80 |
+
gr.Textbox(
|
| 81 |
+
value="You are a friendly Chatbot.",
|
| 82 |
+
label="System message"
|
| 83 |
+
),
|
| 84 |
+
gr.Slider(
|
| 85 |
+
minimum=1,
|
| 86 |
+
maximum=2048,
|
| 87 |
+
value=512,
|
| 88 |
+
step=1,
|
| 89 |
+
label="Max new tokens"
|
| 90 |
+
),
|
| 91 |
+
gr.Slider(
|
| 92 |
+
minimum=0.1,
|
| 93 |
+
maximum=4.0,
|
| 94 |
+
value=0.7,
|
| 95 |
+
step=0.1,
|
| 96 |
+
label="Temperature"
|
| 97 |
+
),
|
| 98 |
+
gr.Slider(
|
| 99 |
+
minimum=0.1,
|
| 100 |
+
maximum=1.0,
|
| 101 |
+
value=0.95,
|
| 102 |
+
step=0.05,
|
| 103 |
+
label="Top-p (nucleus sampling)",
|
| 104 |
+
),
|
| 105 |
+
],
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
# Add API status at the footer
|
| 109 |
+
gr.HTML(
|
| 110 |
+
value=f"<div style='text-align: center; padding: 10px; background-color: #f0f0f0; border-top: 1px solid #ddd;'>{get_api_status()}</div>",
|
| 111 |
+
every=30 # Updates every 30 seconds
|
| 112 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
if __name__ == "__main__":
|
| 115 |
+
demo.launch()
|
|
|
|
|
|