RAYSENSE / app.py
MehmetK's picture
Update app.py
2e6214f verified
import gradio as gr
import numpy as np
import matplotlib.pyplot as plt
from skimage.transform import radon, iradon
from scipy.fft import fft, ifft
from io import BytesIO
import base64
import os
import requests
from PIL import Image
# Hugging Face API ayarları
API_URL = "https://api-inference.huggingface.co/models/openai-community/gpt2-large"
HF_TOKEN = os.environ.get("HF_TOKEN") # Environment variable'dan token'ı al
headers = {"Authorization": f"Bearer {HF_TOKEN}"} # API anahtarını başlıkta gönder
def query_gpt2(payload):
"""Hugging Face API'sine istek gönderir ve yanıtı döner."""
try:
response = requests.post(API_URL, headers=headers, json=payload)
print("Status Code:", response.status_code) # Durum kodunu yazdır
response.raise_for_status() # Hata durumunda istisna fırlatır
response_json = response.json()
print("Response JSON:", response_json) # Yanıtın içeriğini yazdır
return response_json
except requests.exceptions.HTTPError as errh:
print(f"HTTP Error: {errh}")
except requests.exceptions.ConnectionError as errc:
print(f"Error Connecting: {errc}")
except requests.exceptions.Timeout as errt:
print(f"Timeout Error: {errt}")
except requests.exceptions.RequestException as err:
print(f"Something went wrong: {err}")
return {"generated_text": "No response from GPT-2."}
def process_and_query(image):
"""Görüntüyü işleyip GPT-2'ye gönderir."""
# Orijinal görüntüyü işleme (grayscale'e çevirme)
image = image.convert("L") # Grayscale'e çevirme
image = np.array(image)
# Sinogram oluşturma (projeksiyon verileri)
theta = np.linspace(0., 180., max(image.shape), endpoint=False)
sinogram = radon(image, theta=theta, circle=True)
# Sinogram verilerini metin olarak çıkarma
sinogram_text = "\n".join([", ".join(map(str, row)) for row in sinogram])
print("Sinogram Verileri (Text):")
print(sinogram_text)
# GPT-2 modeline sinogram verilerini gönderme
gpt_response = query_gpt2({"inputs": sinogram_text})
gpt_output = gpt_response.get("generated_text", "GPT-2'den yanıt alınamadı.")
# Projeksiyon verilerine Fourier dönüşümü uygulama
fourier = fft(sinogram, axis=0)
# Ramp filtre uygulama
freq = np.fft.fftfreq(sinogram.shape[0]).reshape(-1, 1)
ramp_filter = np.abs(freq)
filtered_fourier = fourier * ramp_filter
# Ters Fourier dönüşümü uygulama
filtered_sinogram = np.real(ifft(filtered_fourier, axis=0))
# Geri yansıtma (back projection) ile görüntü oluşturma
reconstructed_image = iradon(filtered_sinogram, theta=theta, circle=True)
# Görselleştirme için görüntüler
fig, axes = plt.subplots(2, 2, figsize=(10, 10))
axes[0, 0].set_title("Orijinal Görüntü")
axes[0, 0].imshow(image, cmap="gray")
axes[0, 0].axis("off")
axes[0, 1].set_title("Sinogram")
axes[0, 1].imshow(sinogram, cmap="gray", aspect="auto")
axes[0, 1].axis("off")
axes[1, 0].set_title("Filtrelenmiş Sinogram")
axes[1, 0].imshow(filtered_sinogram, cmap="gray", aspect="auto")
axes[1, 0].axis("off")
axes[1, 1].set_title("Rekonstürülen Görüntü")
axes[1, 1].imshow(reconstructed_image, cmap="gray")
axes[1, 1].axis("off")
plt.tight_layout()
# Görüntüleri base64 formatında döndürme
buf = BytesIO()
plt.savefig(buf, format="png")
buf.seek(0)
encoded_image = base64.b64encode(buf.read()).decode("utf-8")
buf.close()
plt.close()
# Görüntü ve sinogram verisini döndürme
return f"<img src='data:image/png;base64,{encoded_image}'/>", sinogram_text, gpt_output
# Gradio arayüzü tanımlama
with gr.Blocks() as demo:
gr.Markdown("# Sinogram Görüntüleme ve GPT-2 İşlemleri")
gr.Markdown("Bir görüntü yükleyin, sinogram verilerini işleyin ve GPT-2'ye gönderin.")
with gr.Row():
image_input = gr.Image(type="pil", label="Görüntü Yükle")
output = gr.HTML(label="Sonuç Görselleştirme")
sinogram_output = gr.Textbox(label="Sinogram Verileri (Text)")
gpt_output = gr.Textbox(label="GPT-2 Yanıtı")
process_button = gr.Button("İşle ve GPT-2'ye Gönder")
process_button.click(process_and_query, inputs=[image_input], outputs=[output, sinogram_output, gpt_output])
if __name__ == "__main__":
demo.launch()