update to use client
Browse files- app_allenai.py +78 -9
app_allenai.py
CHANGED
|
@@ -1,13 +1,82 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
|
| 13 |
|
|
|
|
| 1 |
+
from gradio_client import Client
|
| 2 |
import gradio as gr
|
| 3 |
+
|
| 4 |
+
MODELS = {
|
| 5 |
+
"Olmo": "akhaliq/olmo-anychat",
|
| 6 |
+
"Allen Test": "akhaliq/allen-test"
|
| 7 |
+
}
|
| 8 |
+
|
| 9 |
+
def create_chat_fn(client):
|
| 10 |
+
def chat(message, history):
|
| 11 |
+
response = client.predict(
|
| 12 |
+
message=message,
|
| 13 |
+
system_prompt="You are a helpful AI assistant.",
|
| 14 |
+
temperature=0.7,
|
| 15 |
+
max_new_tokens=1024,
|
| 16 |
+
top_k=40,
|
| 17 |
+
repetition_penalty=1.1,
|
| 18 |
+
top_p=0.95,
|
| 19 |
+
api_name="/chat"
|
| 20 |
+
)
|
| 21 |
+
return response
|
| 22 |
+
return chat
|
| 23 |
+
|
| 24 |
+
def set_client_for_session(model_name: str, request: gr.Request):
|
| 25 |
+
headers = {}
|
| 26 |
+
if request and hasattr(request, 'request') and hasattr(request.request, 'headers'):
|
| 27 |
+
x_ip_token = request.request.headers.get('x-ip-token')
|
| 28 |
+
if x_ip_token:
|
| 29 |
+
headers["X-IP-Token"] = x_ip_token
|
| 30 |
+
|
| 31 |
+
return Client(MODELS[model_name], headers=headers)
|
| 32 |
+
|
| 33 |
+
def safe_chat_fn(message, history, client):
|
| 34 |
+
if client is None:
|
| 35 |
+
return "Error: Client not initialized. Please refresh the page."
|
| 36 |
+
return create_chat_fn(client)(message, history)
|
| 37 |
+
|
| 38 |
+
with gr.Blocks() as interface:
|
| 39 |
+
gr.Markdown("# AI Chat Interface")
|
| 40 |
+
|
| 41 |
+
client = gr.State()
|
| 42 |
+
|
| 43 |
+
model_dropdown = gr.Dropdown(
|
| 44 |
+
choices=list(MODELS.keys()),
|
| 45 |
+
value="Olmo",
|
| 46 |
+
label="Select Model",
|
| 47 |
+
interactive=True
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
chat_interface = gr.ChatInterface(
|
| 51 |
+
fn=safe_chat_fn,
|
| 52 |
+
title="",
|
| 53 |
+
description="Chat with AI",
|
| 54 |
+
examples=[
|
| 55 |
+
["Hello! How are you?", None],
|
| 56 |
+
["What is machine learning?", None]
|
| 57 |
+
],
|
| 58 |
+
cache_examples=False,
|
| 59 |
+
additional_inputs=[client]
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
# Update client when model changes
|
| 63 |
+
def update_model(model_name, request):
|
| 64 |
+
return set_client_for_session(model_name, request)
|
| 65 |
+
|
| 66 |
+
model_dropdown.change(
|
| 67 |
+
fn=update_model,
|
| 68 |
+
inputs=[model_dropdown],
|
| 69 |
+
outputs=[client],
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
# Initialize client on page load
|
| 73 |
+
interface.load(
|
| 74 |
+
fn=set_client_for_session,
|
| 75 |
+
inputs=gr.State("Olmo"), # Default model
|
| 76 |
+
outputs=client,
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
interface.launch(show_error=True)
|
| 80 |
|
| 81 |
|
| 82 |
|