Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import numpy as np
|
| 5 |
+
from transformers import AutoModel, AutoConfig
|
| 6 |
+
from model import Pix2TextModel, Pix2TextConfig
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
# Model yükleme fonksiyonu
|
| 10 |
+
def load_model():
|
| 11 |
+
try:
|
| 12 |
+
# Hugging Face'den model yükle
|
| 13 |
+
config = AutoConfig.from_pretrained("./", trust_remote_code=True)
|
| 14 |
+
model = AutoModel.from_pretrained("./", trust_remote_code=True)
|
| 15 |
+
return model
|
| 16 |
+
except Exception as e:
|
| 17 |
+
print(f"Model yükleme hatası: {e}")
|
| 18 |
+
# Yerel model oluştur
|
| 19 |
+
config = Pix2TextConfig()
|
| 20 |
+
model = Pix2TextModel(config)
|
| 21 |
+
return model
|
| 22 |
+
|
| 23 |
+
model = load_model()
|
| 24 |
+
|
| 25 |
+
def predict_text(image):
|
| 26 |
+
"""Görüntüden metin çıkarma fonksiyonu"""
|
| 27 |
+
try:
|
| 28 |
+
if image is None:
|
| 29 |
+
return "Lütfen bir görüntü yükleyin."
|
| 30 |
+
|
| 31 |
+
# PIL Image'a çevir
|
| 32 |
+
if isinstance(image, np.ndarray):
|
| 33 |
+
image = Image.fromarray(image)
|
| 34 |
+
|
| 35 |
+
# Model ile tahmin yap
|
| 36 |
+
result = model.predict(image)
|
| 37 |
+
|
| 38 |
+
return f"Çıkarılan Metin: {result}"
|
| 39 |
+
|
| 40 |
+
except Exception as e:
|
| 41 |
+
return f"Hata oluştu: {str(e)}"
|
| 42 |
+
|
| 43 |
+
def create_demo():
|
| 44 |
+
"""Gradio demo arayüzü oluştur"""
|
| 45 |
+
|
| 46 |
+
with gr.Blocks(title="Pix2Text - Görüntüden Metin Çıkarma") as demo:
|
| 47 |
+
gr.Markdown("# 🔤 Pix2Text Model Test Arayüzü")
|
| 48 |
+
gr.Markdown("Görüntü yükleyerek metin çıkarma işlemini test edebilirsiniz.")
|
| 49 |
+
|
| 50 |
+
with gr.Row():
|
| 51 |
+
with gr.Column():
|
| 52 |
+
image_input = gr.Image(
|
| 53 |
+
label="Görüntü Yükleyin",
|
| 54 |
+
type="pil",
|
| 55 |
+
height=400
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
predict_btn = gr.Button("Metni Çıkar", variant="primary")
|
| 59 |
+
|
| 60 |
+
with gr.Column():
|
| 61 |
+
output_text = gr.Textbox(
|
| 62 |
+
label="Çıkarılan Metin",
|
| 63 |
+
lines=10,
|
| 64 |
+
placeholder="Sonuç burada görünecek..."
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
# Event handlers
|
| 68 |
+
predict_btn.click(
|
| 69 |
+
fn=predict_text,
|
| 70 |
+
inputs=[image_input],
|
| 71 |
+
outputs=[output_text]
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
# Örnek görüntüler
|
| 75 |
+
gr.Examples(
|
| 76 |
+
examples=[
|
| 77 |
+
["example1.jpg"],
|
| 78 |
+
["example2.png"]
|
| 79 |
+
],
|
| 80 |
+
inputs=[image_input],
|
| 81 |
+
outputs=[output_text],
|
| 82 |
+
fn=predict_text,
|
| 83 |
+
cache_examples=False
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
gr.Markdown("### Model Bilgileri")
|
| 87 |
+
gr.Markdown("""
|
| 88 |
+
- **Model Tipi**: Pix2Text (Görüntüden Metin Çıkarma)
|
| 89 |
+
- **Framework**: PyTorch + Transformers
|
| 90 |
+
- **Desteklenen Formatlar**: JPG, PNG, JPEG
|
| 91 |
+
- **Maksimum Görüntü Boyutu**: 2048x2048 piksel
|
| 92 |
+
""")
|
| 93 |
+
|
| 94 |
+
return demo
|
| 95 |
+
|
| 96 |
+
if __name__ == "__main__":
|
| 97 |
+
demo = create_demo()
|
| 98 |
+
demo.launch(
|
| 99 |
+
server_name="0.0.0.0",
|
| 100 |
+
server_port=7860,
|
| 101 |
+
share=True
|
| 102 |
+
)
|