File size: 1,381 Bytes
ef89921 123a96c ef89921 60a05b6 ef89921 db1b2d9 1aaf316 db1b2d9 1aaf316 794e818 db1b2d9 c97b2e5 123a96c 1aaf316 794e818 123a96c dc008a3 794e818 123a96c 1aaf316 123a96c 1aaf316 123a96c db1b2d9 016a061 db1b2d9 123a96c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
import gradio as gr
import requests
# GPT-2 λμ blenderbot-3B λͺ¨λΈ μ¬μ©
API_URL = "https://api-inference.huggingface.co/models/facebook/blenderbot-3B"
# μ μ λ³μλ‘ API ν€ μ μ₯
api_key_store = {"key": None}
# API ν€ μ μ₯ ν¨μ
def set_api_key(api_key):
api_key_store["key"] = api_key
return "β
API Key Set Successfully!"
# LLM μ§μ ν¨μ
def query_llm(message, history):
api_key = api_key_store["key"]
if not api_key:
return "β Error: API key is missing. Please enter your Hugging Face API key first."
headers = {"Authorization": f"Bearer {api_key}"}
payload = {"inputs": message}
response = requests.post(API_URL, headers=headers, json=payload)
if response.status_code == 200:
return response.json()[0]["generated_text"]
else:
return f"β Error: {response.status_code} - {response.json()}"
# Gradio UI (API ν€ μ
λ ₯ + μ±ν
)
with gr.Blocks() as demo:
with gr.Row():
api_key_input = gr.Textbox(label="Hugging Face API Key", placeholder="Enter your API Key here", type="password")
set_key_button = gr.Button("Set API Key")
api_key_output = gr.Textbox(label="Status", interactive=False) # API ν€ μν νμ
set_key_button.click(set_api_key, inputs=[api_key_input], outputs=[api_key_output])
chatbot = gr.ChatInterface(fn=query_llm)
demo.launch()
|