add huggingface inference providers
Browse filesFiles changed (3)
- app_huggingface.py+15-62
- pyproject.toml+1-1
- requirements.txt+3-2
12124345677891011891013141116171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667121314151617181920import gradio as grfrom gradio_client import Client, handle_fileimport ai_gradio
MODELS = {"SmolVLM-Instruct": "akhaliq/SmolVLM-Instruct"}from utils_ai_gradio import get_app
# Get the hyperbolic models but keep their full names for loadingHUGGINGFACE_MODELS_FULL = [k for k in ai_gradio.registry.keys() if k.startswith("huggingface:")]
def create_chat_fn(client): def chat(message, history): # Extract text and files from the message text = message.get("text", "") files = message.get("files", [])# Create display names without the prefixHUGGINGFACE_MODELS_DISPLAY = [k.replace("huggingface:", "") for k in HUGGINGFACE_MODELS_FULL]
# Handle file uploads if present processed_files = [handle_file(f) for f in files]
response = client.predict( message={"text": text, "files": processed_files}, system_prompt="You are a helpful AI assistant.", temperature=0.7, max_new_tokens=1024, top_k=40, repetition_penalty=1.1, top_p=0.95, api_name="/chat", ) return response
return chat
def set_client_for_session(model_name, request: gr.Request): headers = {} if request and hasattr(request, "headers"): x_ip_token = request.headers.get("x-ip-token") if x_ip_token: headers["X-IP-Token"] = x_ip_token
return Client(MODELS[model_name], headers=headers)
def safe_chat_fn(message, history, client): if client is None: return "Error: Client not initialized. Please refresh the page." try: return create_chat_fn(client)(message, history) except Exception as e: print(f"Error during chat: {e!s}") return f"Error during chat: {e!s}"
with gr.Blocks() as demo: client = gr.State()
model_dropdown = gr.Dropdown( choices=list(MODELS.keys()), value="SmolVLM-Instruct", label="Select Model", interactive=True )
chat_interface = gr.ChatInterface(fn=safe_chat_fn, additional_inputs=[client], multimodal=True)
# Update client when model changes model_dropdown.change(fn=set_client_for_session, inputs=[model_dropdown], outputs=[client])
# Initialize client on page load demo.load(fn=set_client_for_session, inputs=[gr.State("SmolVLM-Instruct")], outputs=[client])
if __name__ == "__main__": demo.launch()# Create and launch the interface using get_app utilitydemo = get_app( models=HUGGINGFACE_MODELS_FULL, # Use the full names with prefix default_model=HUGGINGFACE_MODELS_FULL[0], dropdown_label="Select Huggingface Model", choices=HUGGINGFACE_MODELS_DISPLAY, # Display names without prefix fill_height=True, coder=True,)37 unmodified lines383940414142434437 unmodified lines "langchain>=0.3.14", "chromadb>=0.5.23", "openai>=1.55.0", "ai-gradio[crewai,deepseek,gemini,groq,hyperbolic,openai,smolagents,transformers, langchain, mistral,minimax,nvidia, qwen, openrouter]>=0.2.46", "ai-gradio[crewai,deepseek,gemini,groq,hyperbolic,openai,smolagents,transformers, langchain, mistral,minimax,nvidia, qwen, openrouter, huggingface]>=0.2.47",]
[tool.uv.sources]1 unmodified line23455678419 unmodified lines4284294304314314324334344354364371 unmodified line# uv pip compile pyproject.toml -o requirements.txtaccelerate==1.2.1 # via ai-gradioai-gradio==0.2.46ai-gradio==0.2.47 # via anychat (pyproject.toml)aiofiles==23.2.1 # via gradio419 unmodified lines # langchain-communityhttpx-ws==0.7.1 # via fireworks-aihuggingface-hub==0.27.1huggingface-hub==0.28.1 # via # accelerate # ai-gradio # gradio # gradio-client # tokenizersapp_huggingface.py+15-62
pyproject.toml+1-1
requirements.txt+3-2