星期日, 15 6 月, 2025
No Result
View All Result
AI TAIWAN 台灣人工智慧中心
  • Home
  • AI 綜合新聞
  • AI 自動化與 AI Agents
  • AI 智慧產業
  • 機器學習與應用
  • 自然語言處理
  • 神經連結和腦機接口
  • 機器人與自動化
  • 道德與法規
  • 安全
AI TAIWAN 台灣人工智慧中心
  • Home
  • AI 綜合新聞
  • AI 自動化與 AI Agents
  • AI 智慧產業
  • 機器學習與應用
  • 自然語言處理
  • 神經連結和腦機接口
  • 機器人與自動化
  • 道德與法規
  • 安全
No Result
View All Result
AI TAIWAN 台灣人工智慧中心
No Result
View All Result
Your Ad
Home AI 綜合新聞

建立一個互動式雙語(阿拉伯語和英語)聊天介面,使用 Arcee AI 的開源 Meraj-Mini:利用 GPU 加速、PyTorch、Transformers、Accelerate、BitsAndBytes 和 Gradio

2025-03-13
in AI 綜合新聞
0 0
0
建立一個互動式雙語(阿拉伯語和英語)聊天介面,使用 Arcee AI 的開源 Meraj-Mini:利用 GPU 加速、PyTorch、Transformers、Accelerate、BitsAndBytes 和 Gradio
Share on FacebookShare on Twitter
Your Ad


雙語聊天助手教學

在這個教學中,我們將實現一個由 Arcee 的 Meraj-Mini 模型驅動的雙語聊天助手,並在 Google Colab 上使用 T4 GPU 無縫部署。這個教學展示了開源語言模型的能力,同時提供了一個實用的實作經驗,讓我們能在免費的雲資源下部署最先進的人工智慧解決方案。我們將使用一套強大的工具,包括:

  • Arcee 的 Meraj-Mini 模型
  • Transformers 函式庫用於模型加載和標記化
  • Accelerate 和 bitsandbytes 用於高效量化
  • PyTorch 用於深度學習計算
  • Gradio 用於創建互動式網頁介面

啟用 GPU 加速

!nvidia-smi –query-gpu=name,memory.total –format=csv

!pip install -qU transformers accelerate bitsandbytes
!pip install -q gradio

首先,我們使用 nvidia-smi 命令查詢 GPU 的名稱和總記憶體,以啟用 GPU 加速。接著安裝和更新關鍵的 Python 函式庫,例如 transformers、accelerate、bitsandbytes 和 gradio,以支援機器學習任務和部署互動應用程式。

配置模型

import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline, BitsAndBytesConfig

quant_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type=”nf4″,
bnb_4bit_compute_dtype=torch.float16,
bnb_4bit_use_double_quant=True
)

model = AutoModelForCausalLM.from_pretrained(
“arcee-ai/Meraj-Mini”,
quantization_config=quant_config,
device_map=”auto”
)
tokenizer = AutoTokenizer.from_pretrained(“arcee-ai/Meraj-Mini”)

然後,我們使用 BitsAndBytesConfig 配置 4 位元量化設置,以高效加載模型,並從 Hugging Face 加載 “arcee-ai/Meraj-Mini” 的因果語言模型及其標記器,自動映射設備以獲得最佳性能。

建立聊天管道

chat_pipeline = pipeline(
“text-generation”,
model=model,
tokenizer=tokenizer,
max_new_tokens=512,
temperature=0.7,
top_p=0.9,
repetition_penalty=1.1,
do_sample=True
)

在這裡,我們使用 Hugging Face 的管道函數創建一個專為聊天互動設計的文本生成管道。它配置了最大新標記數、溫度、top_p 和重複懲罰,以平衡文本生成過程中的多樣性和一致性。

定義聊天功能

def format_chat(messages):
prompt = “”
for msg in messages:
prompt += f”<|im_start|>msg[‘role’]nmsg[‘content’]<|im_end|>n”
prompt += “<|im_start|>assistantn”
return prompt

def generate_response(user_input, history=[]):
history.append({“role”: “user”, “content”: user_input})
formatted_prompt = format_chat(history)
output = chat_pipeline(formatted_prompt)[0][‘generated_text’]
assistant_response = output.split(“<|im_start|>assistantn”)[-1].split(“<|im_end|>”)[0]
history.append({“role”: “assistant”, “content”: assistant_response})
return assistant_response, history

我們定義了兩個函數來促進對話介面。第一個函數將聊天歷史格式化為結構化的提示,並使用自定義分隔符,第二個函數則附加新的用戶消息,使用文本生成管道生成回應,並相應地更新對話歷史。

建立網頁聊天介面

import gradio as gr

with gr.Blocks() as demo:
chatbot = gr.Chatbot()
msg = gr.Textbox(label=”Message”)
clear = gr.Button(“Clear History”)

def respond(message, chat_history):
response, _ = generate_response(message, chat_history.copy())
return response, chat_history + [(message, response)]

msg.submit(respond, [msg, chatbot], [msg, chatbot])
clear.click(lambda: None, None, chatbot, queue=False)

demo.launch(share=True)

最後,我們使用 Gradio 建立了一個基於網頁的聊天機器人介面。它創建了聊天歷史、消息輸入和清除歷史按鈕的 UI 元素,並定義了一個回應函數,與文本生成管道整合以更新對話。最後,啟動演示並啟用共享,以便公眾訪問。

這裡是 Colab 筆記本。別忘了在 Twitter 上關注我們,加入我們的 Telegram 頻道和 LinkedIn 群組。還有,記得加入我們的 80k+ ML SubReddit。



新聞來源

本文由 AI 台灣 運用 AI 技術編撰,內容僅供參考,請自行核實相關資訊。
歡迎加入我們的 AI TAIWAN 台灣人工智慧中心 FB 社團,
隨時掌握最新 AI 動態與實用資訊!

Tags: ArceeGPUGradioMerajMini利用加速PyTorchTransformersAccelerateBitsAndBytes和建立一個互動式雙語阿拉伯語和英語聊天介面使用的開源
Previous Post

這篇AI論文介紹了R1-Searcher:一個基於強化學習的框架,用於提升LLM搜索能力

Next Post

靈巧科技獲得9500萬美元資金用於集裝箱卸貨機器人

Related Posts

中國教育改革人工智慧助力創新人才培育
AI 綜合新聞

中國教育改革人工智慧助力創新人才培育

2025-06-11
AI 助力中風患者康復Devon 的 SAMueL-2 計畫創新突破
AI 綜合新聞

AI 助力中風患者康復Devon 的 SAMueL-2 計畫創新突破

2025-04-24
全球AI教育市場蓬勃發展智慧學習工具引領新趨勢
AI 綜合新聞

全球AI教育市場蓬勃發展智慧學習工具引領新趨勢

2025-04-21
2027 年 AI 預測人類水平 AI 的全新里程碑
AI 綜合新聞

2027 年 AI 預測人類水平 AI 的全新里程碑

2025-04-21
AI 技術對人類智能的影響我們在失去什麼?
AI 綜合新聞

AI 技術對人類智能的影響我們在失去什麼?

2025-04-20
人工智慧重塑遊戲開發遊戲未來從現在開始
AI 綜合新聞

人工智慧重塑遊戲開發遊戲未來從現在開始

2025-04-18
Next Post
靈巧科技獲得9500萬美元資金用於集裝箱卸貨機器人

靈巧科技獲得9500萬美元資金用於集裝箱卸貨機器人

Visatronic:一種僅解碼的多模態語音合成模型

Visatronic:一種僅解碼的多模態語音合成模型

發佈留言 取消回覆

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

Archives

  • 2025 年 6 月
  • 2025 年 4 月
  • 2025 年 3 月
  • 2025 年 2 月
  • 2025 年 1 月
  • 2024 年 12 月
  • 2024 年 11 月
  • 2024 年 10 月
  • 2024 年 9 月
  • 2024 年 8 月
  • 2024 年 7 月
  • 2024 年 6 月
  • 2024 年 5 月
  • 2024 年 4 月
  • 2024 年 3 月
  • 2024 年 2 月
  • 2023 年 10 月
  • 2023 年 9 月
  • 2023 年 8 月
  • 2023 年 7 月
  • 2023 年 5 月
  • 2023 年 3 月
  • 2023 年 1 月
  • 2022 年 12 月
  • 2022 年 11 月
  • 2022 年 5 月
  • 2022 年 4 月
  • 2022 年 1 月
  • 2021 年 11 月
  • 2021 年 8 月
  • 2021 年 5 月
  • 2021 年 3 月
  • 2021 年 1 月
  • 2020 年 12 月
  • 2020 年 10 月
  • 2020 年 9 月
  • 2019 年 7 月
  • 2018 年 11 月

Categories

  • AI 智慧產業
  • AI 綜合新聞
  • AI 自動化與 AI Agents
  • 安全
  • 機器人與自動化
  • 機器學習與應用
  • 神經連結和腦機接口
  • 自然語言處理
  • 道德與法規
Your Ad
  • 關於我們
  • 廣告合作
  • 免責聲明
  • 隱私權政策
  • DMCA
  • Cookie 隱私權政策
  • 條款與條件
  • 聯絡我們
AI TAIWAN

版權 © 2024 AI TAIWAN.
AI TAIWAN 對外部網站的內容不負任何責任。

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In
No Result
View All Result
  • Home
  • AI 綜合新聞
  • AI 自動化與 AI Agents
  • AI 智慧產業
  • 機器學習與應用
  • 自然語言處理
  • 神經連結和腦機接口
  • 機器人與自動化
  • 道德與法規
  • 安全

版權 © 2024 AI TAIWAN.
AI TAIWAN 對外部網站的內容不負任何責任。