Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
|
3 |
+
import torch
|
4 |
+
import torch.nn.functional as F
|
5 |
+
|
6 |
+
# model yükleme
|
7 |
+
model_name = "fc63/gp-model-v3"
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
10 |
+
model.eval()
|
11 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
12 |
+
model.to(device)
|
13 |
+
|
14 |
+
# translate pipeline (multilingual → İngilizce)
|
15 |
+
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-mul-en")
|
16 |
+
|
17 |
+
def predict(text, language):
|
18 |
+
original_text = text
|
19 |
+
if language == "Not English":
|
20 |
+
try:
|
21 |
+
translated = translator(text)[0]["translation_text"]
|
22 |
+
except Exception as e:
|
23 |
+
return f"Translation failed: {e}"
|
24 |
+
else:
|
25 |
+
translated = text
|
26 |
+
|
27 |
+
# model inference
|
28 |
+
inputs = tokenizer(translated, return_tensors="pt", truncation=True, padding=True, max_length=128).to(device)
|
29 |
+
with torch.no_grad():
|
30 |
+
outputs = model(**inputs)
|
31 |
+
probs = F.softmax(outputs.logits, dim=1)
|
32 |
+
pred = torch.argmax(probs, dim=1).item()
|
33 |
+
gender = "Female" if pred == 0 else "Male"
|
34 |
+
confidence = round(probs[0][pred].item() * 100, 1)
|
35 |
+
|
36 |
+
return f"{gender} (Confidence: {confidence}%)"
|
37 |
+
|
38 |
+
# interface / arayüz
|
39 |
+
demo = gr.Interface(
|
40 |
+
fn=predict,
|
41 |
+
inputs=[
|
42 |
+
gr.Textbox(label="Enter your text here", lines=4, placeholder="Type something..."),
|
43 |
+
gr.Radio(["English", "Not English"], label="Text Language", value="English")
|
44 |
+
],
|
45 |
+
outputs="text",
|
46 |
+
title="Gender Prediction with DeBERTa",
|
47 |
+
description="Predicts the author's gender from a text. Supports non-English inputs via automatic translation."
|
48 |
+
)
|
49 |
+
|
50 |
+
demo.launch()
|