ANXLOG's picture
Upload 3 files
9d91c12 verified
#!/usr/bin/env python3
"""
LOGOS Hugging Face Space - Clean Deployment
"""
import gradio as gr
import requests
import base64
import time
from io import BytesIO
from PIL import Image, ImageDraw
def analyze_image(image, prompt="Analyze this image"):
"""Analyze image with LM Studio"""
if image is None:
return "Please upload an image first."
try:
# Convert PIL to RGB if needed
if image.mode != 'RGB':
image = image.convert('RGB')
# Encode image
buffer = BytesIO()
image.save(buffer, format='PNG')
img_b64 = base64.b64encode(buffer.getvalue()).decode()
# Send to LM Studio
start_time = time.time()
response = requests.post(
"http://192.168.0.105:1234/v1/chat/completions",
headers={"Content-Type": "application/json"},
json={
"model": "google/gemma-3-4b",
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": prompt},
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{img_b64}"}}
]
}
],
"max_tokens": 500,
"temperature": 0.3
},
timeout=30
)
if response.status_code == 200:
result = response.json()
content = result["choices"][0]["message"]["content"]
response_time = time.time() - start_time
return f"Analysis ({response_time:.1f}s):\n\n{content}"
else:
return f"Error: HTTP {response.status_code}"
except Exception as e:
return f"Error: {str(e)}"
def create_test_image():
"""Create a simple test image"""
img = Image.new('RGB', (224, 224), color='white')
draw = ImageDraw.Draw(img)
draw.rectangle([50, 50, 174, 174], fill='red', outline='black', width=2)
draw.ellipse([87, 87, 137, 137], fill='blue')
draw.text((10, 10), "LOGOS", fill='black')
return img
def test_connection():
"""Test LM Studio connection"""
try:
response = requests.post(
"http://192.168.0.105:1234/v1/chat/completions",
headers={"Content-Type": "application/json"},
json={
"model": "google/gemma-3-4b",
"messages": [{"role": "user", "content": "Say 'Connection test successful'"}],
"max_tokens": 10
},
timeout=10
)
if response.status_code == 200:
result = response.json()
content = result["choices"][0]["message"]["content"]
return f"Connected: {content}"
else:
return f"Connection failed: HTTP {response.status_code}"
except Exception as e:
return f"Connection error: {str(e)}"
# Create Gradio interface
with gr.Blocks(title="LOGOS Computational System") as interface:
gr.HTML("""
<div style="text-align: center; padding: 20px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; border-radius: 10px; margin-bottom: 20px;">
<h1>LOGOS Computational System</h1>
<p>Vision Analysis with LM Studio Integration</p>
</div>
""")
with gr.Row():
with gr.Column(scale=2):
# Input section
gr.Markdown("## Image Analysis")
with gr.Row():
image_input = gr.Image(
label="Upload Image",
type="pil",
height=300
)
with gr.Column():
gr.Button("Create Test Image").click(
fn=create_test_image,
outputs=image_input
)
prompt_input = gr.Textbox(
label="Analysis Prompt",
value="Analyze this image in detail",
lines=3
)
analyze_btn = gr.Button("Analyze Image", variant="primary", size="lg")
# Output
result_output = gr.Textbox(
label="Analysis Result",
lines=12,
interactive=False
)
analyze_btn.click(
fn=analyze_image,
inputs=[image_input, prompt_input],
outputs=result_output
)
with gr.Column(scale=1):
# Status section
gr.Markdown("## System Status")
with gr.Row():
conn_btn = gr.Button("Test Connection", size="sm")
conn_result = gr.Textbox(label="Connection Test", lines=2)
conn_btn.click(
fn=test_connection,
outputs=conn_result
)
gr.Markdown("## Configuration")
gr.JSON(
value={
"lm_studio_url": "http://192.168.0.105:1234",
"model": "google/gemma-3-4b"
},
label="System Config"
)
if __name__ == "__main__":
print("Starting LOGOS Hugging Face Space...")
print("URL: http://localhost:7860")
print("Model: google/gemma-3-4b")
print("LM Studio: http://192.168.0.105:1234")
print()
interface.launch(
server_name="0.0.0.0",
server_port=7860,
share=False,
show_error=True
)