File size: 6,677 Bytes
4fe274c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bd18b9a
c2fe5ff
 
4fe274c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import streamlit as st
from openai import OpenAI
from PIL import Image
import io
import base64

st.set_page_config(page_title="Intelligent OCR & Insight", layout="centered", page_icon="🖼️")

st.title("🖼️ Intelligent Image Text Extraction")
st.write("Extract, translate, or summarize text using **meta-llama/llama-3.2-11b-vision-instruct**")

# --- 1. AKILLI API ANAHTARI YÖNETİMİ ---
DEFAULT_API_KEY = "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"

try:
    hf_api_key = st.secrets.get("OPENROUTER_API_KEY", "")
except Exception:
    hf_api_key = ""

# Sol menü (Sidebar) Yapılandırması
with st.sidebar:
    st.header("🔑 API Configuration")
    user_api_key = st.text_input(
        "Custom OpenRouter API Key (Optional)", 
        type="password", 
        help="The system uses a built-in key by default. If the system key reaches its limit, please provide your own key here."
    )
    
    if user_api_key:
        st.success("🎯 Using your custom API key.")
    else:
        st.info("⚡ Using system default API key.")
    
    st.markdown("---")
    st.header("⚙️ Task Settings")
    
    # Seçenekleri ham LLaMA halüsinasyonlarını engelleyecek şekilde optimize ettik
    task_type = st.selectbox(
        "Choose Action",
        ["Direct Text Extraction", "Extract & Translate to Turkish", "Extract & Summarize"]
    )

active_api_key = user_api_key if user_api_key else (hf_api_key if hf_api_key else DEFAULT_API_KEY)

# --- KRİTİK DÜZELTME: LLaMA için kesin sınırlandırılmış promptlar ---
prompt_dict = {
    "Direct Text Extraction": (
        "You are an expert OCR system. Task: Analyze the image and extract all visible text. "
        "Rules: Return ONLY the raw extracted text exactly as it appears in the image. Do not include any explanations, "
        "do not introduce code snippets, do not add introductory phrases like 'Here is the text', and do not output random characters."
    ),
    "Extract & Translate to Turkish": (
    "You are an expert translator. Task: Read the text within the image and translate it line-by-line into fluent Turkish. "
    "Rules: Output ONLY the direct Turkish translation of the document text. Do not summarize it, do not write a description about the letter, "
    "and do not add conversational filler."
    ),
    "Extract & Summarize": (
        "You are an advanced text analytics system. Task: Extract the text from the image and provide a highly structured, "
        "concise summary using bullet points. Rules: Output ONLY the summary. Do not include meta-commentary or technical jargon."
    )
}

# --- 2. GÖRSEL YÜKLEME VE BASE64 ---
uploaded_file = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])

def encode_image_to_base64(pil_image):
    buffered = io.BytesIO()
    pil_image.save(buffered, format="PNG")
    return base64.b64encode(buffered.getvalue()).decode('utf-8')

if uploaded_file is not None:
    image = Image.open(uploaded_file)
    st.image(image, caption="Uploaded Image", use_container_width=True)

    if st.button("🔍 Process Image"):
        with st.spinner("LLaMA Vision is analyzing the image..."):
            try:
                client = OpenAI(base_url="https://openrouter.ai/api/v1", api_key=active_api_key)
                base64_image = encode_image_to_base64(image)
                
                response = client.chat.completions.create(
                    model="meta-llama/llama-3.2-11b-vision-instruct",
                    messages=[
                        {
                            "role": "user",
                            "content": [
                                {"type": "text", "text": prompt_dict[task_type]},
                                {
                                    "type": "image_url",
                                    "image_url": {"url": f"data:image/png;base64,{base64_image}"}
                                }
                            ]
                        }
                    ],
                    temperature=0.1 # Düşük sıcaklık: Modelin uydurmasını/halüsinasyon görmesini engeller, netliği artırır!
                )

                extracted_text = response.choices[0].message.content.strip()

                st.subheader("📄 Analysis Result")
                st.text_area("Result Output", extracted_text, height=300)
                
                st.download_button(
                    label="📥 Download Result as .txt",
                    data=extracted_text,
                    file_name="llama_vision_output.txt",
                    mime="text/plain"
                )

            except Exception as e:
                st.error("❌ An error occurred during processing.")
                st.warning(
                    "⚠️ **Quota or Connection Limit Reached:** The built-in shared API key might have run out of credits. "
                    "To continue using the app without interruption, please generate a free/paid key on **OpenRouter.ai** "
                    "and paste it into the **🔑 API Configuration** section in the left sidebar."
                )
                with st.expander("Show technical error details"):
                    st.code(str(e))

# --- SAYFA ALTI (FOOTER) ---
st.markdown("---")
st.markdown("""

### 🚀 Real-World Applications & Use Cases

This vision-language system leveraging **LLaMA 3.2 Vision** goes beyond traditional OCR by understanding the context, layout, and meaning of the text within images.



* **📂 Document Digitization:** Auto-extract tabular data, invoices, and forms directly into structured text.

* **🏥 Healthcare Admin:** Convert scanned doctor prescriptions and reports into digital medical data.

* **🛒 Retail & Logistics:** Process invoices or scan warehouse package labels automatically.

* **🎓 Academic Research:** Convert handwritten study notes or whiteboard layouts into clean text.

* **🏭Intelligent Industrial Data Pipelines & Smart Logistics

* **Context-Aware Quality Control:** Integrates object detection with semantic extraction. For instance, after a vision model identifies an item (e.g., specific produce, automotive part, or electronic kit), this pipeline parses labels to extract critical context like **expiration dates**, **serial numbers**, or **country of origin**.

* **End-to-End Supply Chain Traceability:** Automates compliance checks and inventory logging at shipping hubs without human intervention, reducing friction in dynamic warehouse environments.

""")