qiankun commited on
Commit
5e1197b
·
1 Parent(s): b2766e7

Refactor app to implement multilingual sentiment analysis using Hugging Face's pipeline, replacing the previous prompt enhancement functionality. Update Gradio interface and adjust requirements for dependencies.

Browse files
Files changed (2) hide show
  1. app.py +31 -18
  2. requirements.txt +2 -2
app.py CHANGED
@@ -1,29 +1,42 @@
1
- import os
2
  import gradio as gr
3
- from huggingface_hub import InferenceClient
4
 
5
- # 使Space Secrets 管理 HF_TOKEN,而不是明文写在代码里
6
- HF_TOKEN = os.environ.get("HF_TOKEN", "your_hf_token_here")
7
 
8
- def enhance_prompt(prompt):
9
- client = InferenceClient(token=HF_TOKEN, model="PromptEnhancer/PromptEnhancer-Img2img-Edit")
10
 
 
11
  try:
12
- # 使用 text_generation 方法而不是直接调用 client()
13
- response = client.text_generation(prompt)
14
- # 有些模型返回可能在 'generated_text' 或 'enhanced_prompt' 字段里
15
- return response.get("enhanced_prompt") or response.get("generated_text") or "No enhanced prompt returned."
 
 
 
 
 
 
 
 
 
16
  except Exception as e:
17
  return f"Error: {str(e)}"
18
 
19
- with gr.Blocks(title="Prompt Enhancer") as app:
20
- gr.Markdown("## Prompt Enhancer\nEnhance your prompts using the Hugging Face Prompt Enhancer model.")
 
21
 
22
- prompt_input = gr.Textbox(label="Enter your prompt here")
23
- output = gr.Textbox(label="Enhanced Prompt")
24
-
25
- btn = gr.Button("Enhance Prompt")
26
- btn.click(fn=enhance_prompt, inputs=prompt_input, outputs=output)
 
 
 
27
 
28
  if __name__ == "__main__":
29
- app.launch()
 
 
1
  import gradio as gr
2
+ from transformers import pipeline
3
 
4
+ # 直接引Hugging Face Hub 上的模型
5
+ MODEL_REPO = "tabularisai/multilingual-sentiment-analysis"
6
 
7
+ # 初始化情感分析 pipeline
8
+ classifier = pipeline("sentiment-analysis", model=MODEL_REPO)
9
 
10
+ def analyze_sentiment(text):
11
  try:
12
+ results = classifier(text)
13
+ # 处理单条和多条输入
14
+ if isinstance(results, list):
15
+ output_lines = []
16
+ for result in results:
17
+ label = result.get('label', '')
18
+ score = result.get('score', 0)
19
+ output_lines.append(f"Label: {label}, Confidence: {score:.2%}")
20
+ return "\n".join(output_lines)
21
+ else:
22
+ label = results.get('label', '')
23
+ score = results.get('score', 0)
24
+ return f"Label: {label}, Confidence: {score:.2%}"
25
  except Exception as e:
26
  return f"Error: {str(e)}"
27
 
28
+ # Gradio Web 界面
29
+ with gr.Blocks(title="Multilingual Sentiment Analysis") as app:
30
+ gr.Markdown("## 🌍 Multilingual Sentiment Analysis\nAnalyze sentiment for texts in multiple languages using [tabularisai/multilingual-sentiment-analysis](https://huggingface.co/tabularisai/multilingual-sentiment-analysis).")
31
 
32
+ prompt_input = gr.Textbox(label="Enter your text here", lines=3, placeholder="Type or paste your sentence...")
33
+ output = gr.Textbox(label="Sentiment Result", lines=2)
34
+ btn = gr.Button("Analyze Sentiment")
35
+ btn.click(
36
+ fn=analyze_sentiment,
37
+ inputs=prompt_input,
38
+ outputs=output
39
+ )
40
 
41
  if __name__ == "__main__":
42
+ app.launch()
requirements.txt CHANGED
@@ -2,5 +2,5 @@ torch
2
  transformers
3
  Pillow
4
 
5
- gradio>=4.44.0
6
- huggingface_hub>=0.24.6
 
2
  transformers
3
  Pillow
4
 
5
+ gradio
6
+ huggingface_hub