ozgur1 commited on
Commit
8ab06e9
·
verified ·
1 Parent(s): e436cb4

Update app.py

Browse files

API endpoint eklemesi

Files changed (1) hide show
  1. app.py +24 -15
app.py CHANGED
@@ -3,10 +3,8 @@ from transformers import AutoModelForSequenceClassification, AutoTokenizer
3
  import torch
4
  import torch.nn.functional as F
5
 
6
- # 🌍 Çok dilli sentiment analizi modeli
7
  model_name = "cardiffnlp/twitter-xlm-roberta-base-sentiment"
8
-
9
- # Model ve tokenizer
10
  tokenizer = AutoTokenizer.from_pretrained(model_name)
11
  model = AutoModelForSequenceClassification.from_pretrained(model_name)
12
 
@@ -31,17 +29,28 @@ def analyze_text(text):
31
  "empty": "💬"
32
  }
33
 
34
- return {"label": top_label, "emoji": emoji_map[top_label], "scores": scores}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
- # Gradio arayüzü
37
- iface = gr.Interface(
38
- fn=analyze_text,
39
- inputs=gr.Textbox(label="Metin Gir / Enter Text"),
40
- outputs="json",
41
- title="🌍 Multilingual Sentiment Analyzer",
42
- description="Analyzes text sentiment in multiple languages (English, Turkish, etc.)",
43
- )
44
 
45
- # 🔹 Düzeltilmiş launch
46
- iface.queue() # çoklu isteklerde sıraya almak için (isteğe bağlı)
47
- iface.launch(server_name="0.0.0.0", server_port=7860) # artık enable_queue yok
 
3
  import torch
4
  import torch.nn.functional as F
5
 
6
+ # Model
7
  model_name = "cardiffnlp/twitter-xlm-roberta-base-sentiment"
 
 
8
  tokenizer = AutoTokenizer.from_pretrained(model_name)
9
  model = AutoModelForSequenceClassification.from_pretrained(model_name)
10
 
 
29
  "empty": "💬"
30
  }
31
 
32
+ return {
33
+ "label": top_label,
34
+ "emoji": emoji_map[top_label],
35
+ "scores": scores
36
+ }
37
+
38
+ # ✅ API olarak çalışan bir endpoint
39
+ def api_predict(text: str):
40
+ result = analyze_text(text)
41
+ return result
42
+
43
+ with gr.Blocks() as demo:
44
+ gr.Markdown("## 🌍 Multilingual Sentiment Analyzer")
45
+
46
+ with gr.Row():
47
+ input_text = gr.Textbox(label="Metin")
48
+ output_json = gr.JSON(label="Sonuç")
49
+
50
+ input_text.change(fn=analyze_text, inputs=input_text, outputs=output_json)
51
 
52
+ # API endpoint: /api/predict
53
+ demo.add_api_route("/api/predict", api_predict, methods=["POST"])
 
 
 
 
 
 
54
 
55
+ demo.queue()
56
+ demo.launch(server_name="0.0.0.0", server_port=7860)