Nn / app.py
Sakalti's picture
Update app.py
20c59dd verified
import os
import requests
import gradio as gr
# 環境変数からHugging FaceのAPIトークンを取得
token = os.environ.get("HF_TOKEN")
# Hugging Face APIを使ってQwen2.5-7B-Instructモデルに問い合わせる関数
def chat_with_model(message):
url = "https://api-inference.huggingface.co/models/Qwen/Qwen2.5-7B-Instruct"
headers = {"Authorization": f"Bearer {token}"}
data = {"inputs": message}
# APIリクエストを送信
response = requests.post(url, headers=headers, json=data)
# 成功した場合、生成されたテキストを返す
if response.status_code == 200:
return response.json()[0]['generated_text']
else:
return "エラーが発生しました。もう一度試してください。"
# Gradioインターフェースの作成
def chatbot_interface(message):
return chat_with_model(message)
# Gradioインターフェース設定
interface = gr.Interface(
fn=chatbot_interface, # チャットボットの関数
inputs=gr.Textbox(label="メッセージ", placeholder="ここにメッセージを入力してください...", lines=2), # 入力ボックス
outputs="text", # 出力タイプ:テキスト
live=False, # ボタンを押すまで処理を待つ
title="Qwen2.5-7B-Instruct チャットボット", # アプリケーションのタイトル
description="Hugging Face APIを使ってQwen2.5-7B-Instructモデルと対話できます。" # 説明
)
# インターフェースの起動
if __name__ == "__main__":
interface.launch()