Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,36 +1,55 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
# 文本生成
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
| 6 |
# 文本分類 (情感分析)
|
| 7 |
classifier = pipeline('sentiment-analysis', model='distilbert-base-uncased-finetuned-sst-2-english')
|
|
|
|
| 8 |
# 問答
|
| 9 |
qa_pipeline = pipeline('question-answering', model='distilbert-base-cased-distilled-squad')
|
|
|
|
| 10 |
# 翻譯 (英翻法)
|
| 11 |
translator = pipeline('translation_en_to_fr', model='t5-small')
|
| 12 |
|
| 13 |
-
def generate_text(prompt):
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
def classify_text(text):
|
|
|
|
|
|
|
| 17 |
return classifier(text)
|
| 18 |
|
| 19 |
def answer_question(context, question):
|
|
|
|
|
|
|
| 20 |
return qa_pipeline(question=question, context=context)['answer']
|
| 21 |
|
| 22 |
def translate_text(text):
|
|
|
|
|
|
|
| 23 |
return translator(text)[0]['translation_text']
|
| 24 |
|
| 25 |
with gr.Blocks() as demo:
|
| 26 |
gr.Markdown("## Hugging Face Pipeline 互動教學")
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
| 34 |
|
| 35 |
with gr.Tab("情感分析 (Sentiment Analysis)"):
|
| 36 |
with gr.Row():
|
|
@@ -54,4 +73,6 @@ with gr.Blocks() as demo:
|
|
| 54 |
btn_trans = gr.Button("翻譯")
|
| 55 |
btn_trans.click(translate_text, inputs=text_input_trans, outputs=text_output_trans)
|
| 56 |
|
| 57 |
-
#
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
+
import torch # 建議也 import torch,確保後端載入
|
| 4 |
+
|
| 5 |
+
# 檢查是否有可用的 GPU,若無則使用 CPU
|
| 6 |
+
# device = 0 if torch.cuda.is_available() else -1
|
| 7 |
|
| 8 |
# 文本生成
|
| 9 |
+
# 註:GPT-2 較大,在免費的 CPU a環境上可能較慢或超時
|
| 10 |
+
# 為了穩定運行,可以換成更小的模型,或是在此範例中暫時註解掉
|
| 11 |
+
# generator = pipeline('text-generation', model='gpt2', device=device)
|
| 12 |
+
|
| 13 |
# 文本分類 (情感分析)
|
| 14 |
classifier = pipeline('sentiment-analysis', model='distilbert-base-uncased-finetuned-sst-2-english')
|
| 15 |
+
|
| 16 |
# 問答
|
| 17 |
qa_pipeline = pipeline('question-answering', model='distilbert-base-cased-distilled-squad')
|
| 18 |
+
|
| 19 |
# 翻譯 (英翻法)
|
| 20 |
translator = pipeline('translation_en_to_fr', model='t5-small')
|
| 21 |
|
| 22 |
+
# def generate_text(prompt):
|
| 23 |
+
# # 處理可能為空的輸入
|
| 24 |
+
# if not prompt:
|
| 25 |
+
# return "請輸入一些文字..."
|
| 26 |
+
# return generator(prompt, max_length=50, num_return_sequences=1)[0]['generated_text']
|
| 27 |
|
| 28 |
def classify_text(text):
|
| 29 |
+
if not text:
|
| 30 |
+
return {}
|
| 31 |
return classifier(text)
|
| 32 |
|
| 33 |
def answer_question(context, question):
|
| 34 |
+
if not context or not question:
|
| 35 |
+
return "背景文章和問題都不能為空"
|
| 36 |
return qa_pipeline(question=question, context=context)['answer']
|
| 37 |
|
| 38 |
def translate_text(text):
|
| 39 |
+
if not text:
|
| 40 |
+
return ""
|
| 41 |
return translator(text)[0]['translation_text']
|
| 42 |
|
| 43 |
with gr.Blocks() as demo:
|
| 44 |
gr.Markdown("## Hugging Face Pipeline 互動教學")
|
| 45 |
|
| 46 |
+
# 由於 text-generation 在免費 CPU 環境可能不穩定,暫時移除以確保 App 能啟動
|
| 47 |
+
# with gr.Tab("文本生成 (Text Generation)"):
|
| 48 |
+
# with gr.Row():
|
| 49 |
+
# text_input_gen = gr.Textbox(label="輸入開頭文字 (Prompt)")
|
| 50 |
+
# text_output_gen = gr.Textbox(label="模型生成結果")
|
| 51 |
+
# btn_gen = gr.Button("生成")
|
| 52 |
+
# btn_gen.click(generate_text, inputs=text_input_gen, outputs=text_output_gen)
|
| 53 |
|
| 54 |
with gr.Tab("情感分析 (Sentiment Analysis)"):
|
| 55 |
with gr.Row():
|
|
|
|
| 73 |
btn_trans = gr.Button("翻譯")
|
| 74 |
btn_trans.click(translate_text, inputs=text_input_trans, outputs=text_output_trans)
|
| 75 |
|
| 76 |
+
# --- 這就是解決問題的關鍵 ---
|
| 77 |
+
# 啟動 Gradio 應用程式
|
| 78 |
+
demo.launch()
|