Wednesday, September 17, 2025

docker-compose.yml for backend ALONE

Run your FastAPI backend in one container and later connect it with Ollama (running either locally or in another container), here’s a simple docker-compose.yml setup.


✅ docker-compose.yml

version: "3.9"

services:
  backend:
    build: .
    container_name: company-recommender-backend
    ports:
      - "8000:8000"
    environment:
      - OLLAMA_URL=http://ollama:11434  # backend talks to ollama service
    depends_on:
      - ollama

  ollama:
    image: ollama/ollama:latest
    container_name: ollama
    ports:
      - "11434:11434"
    volumes:
      - ollama_data:/root/.ollama  # persist models

volumes:
  ollama_data:

✅ Changes in your main.py

Right now you hardcoded:

ollama_url = "https://immai.acintia.com"

Update this to read from an environment variable so it works inside Docker:

import os

ollama_url = os.getenv("OLLAMA_URL", "http://localhost:11434")

That way:

  • If you run with docker-compose, it will connect to the ollama container.

  • If you run locally without Docker, it defaults to http://localhost:11434.


✅ Usage

From your project root:

docker-compose up --build

Then visit:

http://localhost:8000

Your backend will talk to the Ollama container automatically 🎯.



Dockerize CAAI_AI (As per Request from Maran)

 This supplements to CAAI_AI repository

Dockerfile you can use to dockerize your FastAPI backend (main.py). I’ll assume your project structure looks like this:

project-root/
│── main.py
│── requirements.txt
│── Dockerfile

✅ Dockerfile.txt

# Use an official lightweight Python image
FROM python:3.11-slim

# Set environment variables
ENV PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1

# Set work directory
WORKDIR /app

# Install system dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential curl git && \
    rm -rf /var/lib/apt/lists/*

# Copy requirements first for efficient caching
COPY requirements.txt .

# Install Python dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Copy the entire project
COPY . .

# Expose FastAPI port
EXPOSE 8000

# Run the FastAPI app with uvicorn
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

✅ requirements.txt

If you don’t already have one, here’s what your requirements.txt should contain based on your code:

fastapi
uvicorn[standard]
pydantic
langchain
langchain-community
langgraph

(If you know extra versions you want pinned, you can add == versions.)


✅ Build & Run Docker Container

From your project root:

# Build the image
docker build -t company-recommender-backend .

# Run the container
docker run -d -p 8000:8000 company-recommender-backend

⚡ Your FastAPI backend will now be available at:

http://localhost:8000

Response :



Thursday, September 11, 2025

Ollama in K8S (Name space : immai) [Note the Time Also @ end]

Jeyachandran configured and installed Ollama and given the URL (as given below)

Step 1: To Check the Ollama Running In browser Type

                     http://167.86.108.113:30007/ 

                     Response :  Ollama is running

Step 2: in VS code run this Python Script:

import requests
import time
import json

# Ollama server endpoint
url = "https://immai.acintia.com/api/generate"

# Prompt to test
prompt = "Explain quantum computing in simple words"

payload = {
    "model": "gemma3:4b",
    "prompt": prompt,
    "stream": True   # set to False if you want the whole output at once
}

# Start timer
start_time = time.time()

response = requests.post(url, json=payload, stream=True)

output_text = ""
for line in response.iter_lines():
    if line:
        data = json.loads(line.decode("utf-8"))
        if "response" in data:
            output_text += data["response"]

# End timer
end_time = time.time()
elapsed = end_time - start_time

print("=== Prompt ===")
print(prompt)
print("\n=== Response ===")
print(output_text.strip())
print(f"\n⏱ Response time: {elapsed:.2f} seconds")

Response:
PS F:\xampp\htdocs\wp-plugins\EduSite> & C:/Users/AURMC/AppData/Local/Microsoft/WindowsApps/python3.11.exe f:/xampp/htdocs/wp-plugins/EduSite/test.py
=== Prompt ===
Explain quantum computing in simple words

=== Response ===
Okay, let's break down quantum computing in a way that's hopefully easy to understand. It's a really complex field, but here’s the gist:

**1. Regular Computers (Classical Computers):**

* **Bits:** Think of a regular computer as using tiny switches that are either **on** (1) or **off** (0). Everything a regular computer does – from browsing the internet to playing games – is based on these 0s and 1s.
* **One thing at a time:**  A bit can only be in one state (0 or 1) at any given moment.  It’s like a light switch – it's either on *or* off, but not both at the same time.


**2. Quantum Computers:**

* **Qubits:** Instead of bits, quantum computers use **qubits**. These are based on the weird rules of quantum mechanics.
* **Superposition:** This is the key! A qubit can be **both 0 and 1 *at the same time***. Think of it like a spinning coin – it’s neither heads nor tails until you stop it and look.  This "both at once" ability is called superposition.
* **Entanglement:**  This is even weirder.  When two qubits are entangled, they become linked in a spooky way.  If you measure the state of one, you instantly know the state of the other, no matter how far apart they are.

**3. What does this all mean?**

* **Massive Parallelism:** Because a qubit can be in multiple states at once, a quantum computer can explore many possibilities simultaneously.  A regular computer has to try each option one after another.  This allows quantum computers to potentially solve certain problems *much* faster than classical computers.
* **Specific Problems:** Quantum computers aren't going to replace your laptop. They're designed for *specific* types of problems that are incredibly complex for classical computers.  These include:        
    * **Drug Discovery:** Simulating molecules to find new medicines.
    * **Materials Science:** Designing new materials with specific properties.

**4. Analogy Time:**

Imagine trying to find your way through a maze.

* **Classical Computer:**  You try one path, if it's a dead end, you backtrack and try another.
* **Quantum Computer:** You explore *all* the paths simultaneously!

**Important Note:** Quantum computing is still in its very early stages. Building and programming quantum computers is incredibly difficult.  They are fragile and prone to errors.

---

**Resources to learn more:**

* **IBM Quantum Experience:** [https://quantum.ibm.com/](https://quantum.ibm.com/) – A great place to experiment with real quantum computers.
* **Wikipedia - Quantum Computing:** [https://en.wikipedia.org/wiki/Quantum_computing](https://en.wikipedia.org/wiki/Quantum_computing)


Do you want me to delve into a specific aspect of quantum computing, like:

*   Superposition in more detail?
*   Entanglement?
*   What types of problems are most suited for quantum computers?

⏱ Response time: 158.22 seconds


After Changing URL (-ip address) add adding Marans prompt template Response + UI

import streamlit as st
import requests
import time
import json

# Default Ollama server endpoint
DEFAULT_URL = "https://immai.acintia.com/api/generate"

st.set_page_config(page_title="AI Legal Assistant (India)", layout="wide")
st.title("⚖️ AI Legal Assistant (Indian Laws & Corporate Regulations)")

# Sidebar for settings
st.sidebar.header("Settings")
url = st.sidebar.text_input("Ollama API URL", DEFAULT_URL)
model = st.sidebar.text_input("Model", "gemma3:4b")

# ✅ Initialize conversation history in session state
if "history" not in st.session_state:
    st.session_state["history"] = []

# Prompt input
query = st.text_area("Enter your legal query:", height=100)

# Template
PROMPT_TEMPLATE = (
    "You are an AI Legal Assistant specialized only in Indian laws and corporate regulations. "
    "You must not answer any queries unrelated to Indian legal and corporate law. "
    "Provide a brief summary (2-3 bullet points) by default. If the user asks for more details, "
    "a deeper explanation, or a follow-up question, provide a thorough response based on the conversation history. "
    "Use **bold**, *italics*, bullet points (e.g., - Bullet), and emojis where appropriate. "
    "Current conversation:\n{history}\n"
    "--- Examples ---\n"
    "User: What are the documents required for private limited company registration in India?\n"
    "Legal Assistant: \n"
    "- **Identity Proof**: PAN card, passport, Aadhar card, or driver's license of all directors and shareholders.\n"
    "- **Address Proof**: Voter ID, passport, or a recent utility bill (less than 2 months old).\n"
    "- **Registered Office Proof**: Rent agreement or sale deed + NOC from landlord.\n"
    "- **DIN** and **DSC** for all directors.\n\n"
    "User: What is the weather like today in Tokyo?\n"
    "Legal Assistant: I'm sorry, I can only provide information related to Indian laws and corporate regulations.\n"
    "--- End of Examples ---\n"
    "User: {query}\n\nLegal Assistant:"
)

if st.button("Ask Legal Assistant"):
    if not query.strip():
        st.warning("⚠️ Please enter a query")
    else:
        # Format conversation history
        history_str = "\n".join(
            [f"User: {h['user']}\nLegal Assistant: {h['assistant']}" for h in st.session_state["history"]]
        )

        # Final prompt with history + template
        final_prompt = PROMPT_TEMPLATE.format(history=history_str, query=query)

        payload = {
            "model": model,
            "prompt": final_prompt,
            "stream": True
        }

        st.info("⏳ Generating response...")

        start_time = time.time()
        try:
            response = requests.post(url, json=payload, stream=True, timeout=600)

            output_text = ""
            text_box = st.empty()

            for line in response.iter_lines():
                if line:
                    data = json.loads(line.decode("utf-8"))
                    if "response" in data:
                        output_text += data["response"]
                        text_box.markdown(f"**Response (streaming):**\n\n{output_text}")

            end_time = time.time()
            elapsed = end_time - start_time

            st.success(f"✅ Done in {elapsed:.2f} seconds")

            # Store in history
            st.session_state["history"].append({"user": query, "assistant": output_text.strip()})

            st.markdown("### Final Response")
            st.write(output_text.strip())

        except Exception as e:
            st.error(f"Error: {e}")

# ✅ Safe check for history before showing
if st.session_state.get("history"):
    st.markdown("## 📝 Conversation History")
    for h in st.session_state["history"]:
        st.markdown(f"**User:** {h['user']}")
        st.markdown(f"**Assistant:** {h['assistant']}")
        st.markdown("---")






Friday, April 25, 2025

MCP Agent for Name Checking MCA Portal

Perfect — if you're using Ollama to run local LLMs (like llama3, mistral, or custom models), you can absolutely build an agent-based system that checks company name availability using the Ministry of Corporate Affairs (MCA), India site.


🧠 Goal:

Create a local AI agent (powered by an Ollama model) that:

  1. Accepts a company name as input.

  2. Opens the MCA name check website.

  3. Inputs the name.

  4. Returns whether the name is available or not.


🛠️ Tools & Tech Stack

Component Purpose
Ollama Runs the LLM (e.g., llama3, mistral)
Python Main script + automation
Selenium / Playwright For web interaction (headless browser)
LangChain (Optional) Framework to wrap the agent logic
FastAPI (Optional) API to call agent locally

🧩 Step-by-Step Implementation

1. 🧠 Run LLM Locally with Ollama

Install Ollama and run a model:

ollama run mistralbye

Or start a background service:

ollama serve

You can load any model, like:

ollama run llama3

2. 🐍 Build the Agent Logic in Python

You’ll want to build a tool-using agent that lets the LLM instruct actions. Here's a basic script:

mca_agent.py

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
import time

def check_company_name_availability(company_name):
    options = Options()
    options.add_argument("--headless")
    driver = webdriver.Chrome(options=options)

    driver.get("https://www.mca.gov.in/mcafoportal/showCheckCompanyName.do")

    # MCA site sometimes loads slowly
    time.sleep(3)

    input_box = driver.find_element(By.ID, "companyName")
    input_box.send_keys(company_name)

    # Submit the form
    driver.find_element(By.ID, "companySubmit").click()

    # Wait for result to render
    time.sleep(3)

    # Extract the result message
    try:
        result_element = driver.find_element(By.ID, "companyResult")
        result = result_element.text
    except:
        result = "Unable to fetch result or page layout changed."

    driver.quit()
    return result

3. 🧠 Connect This to Ollama via a Local LLM Agent

You can create a lightweight LLM interface like this:

llm_agent.py

import subprocess
import json
from mca_agent import check_company_name_availability

def run_llm(prompt: str):
    """Use ollama locally to run a prompt and return a reply"""
    process = subprocess.run(
        ["ollama", "run", "mistral"],
        input=prompt.encode(),
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE
    )
    return process.stdout.decode()

def main():
    while True:
        user_input = input("🧑‍💼 Enter company name to check availability: ")
        if user_input.lower() in ["exit", "quit"]:
            break

        # Use the LLM to interpret or enrich the prompt (optional)
        system_prompt = f"""
        The user wants to check if the company name "{user_input}" is available for registration in India via the MCA portal.
        Just confirm and explain what will happen next. 
        """

        print(run_llm(system_prompt))

        # Actually run the tool
        result = check_company_name_availability(user_input)
        print(f"✅ MCA Portal Result:\n{result}\n")

if __name__ == "__main__":
    main()

4. ✅ Example Run

🧑‍💼 Enter company name to check availability: TechNova Solutions
🤖 LLM: Okay, I will check if "TechNova Solutions" is available for registration via the MCA portal.

✅ MCA Portal Result:
The name 'TechNova Solutions' is already in use or too similar to an existing company.

Optional: Use LangChain for Agent-Orchestration

You can also plug this into a LangChain Agent with tools:

  • Tool: CheckMCANameTool

  • Agent: LLM with tool access

  • Prompt: "Is 'ZebraSoft India' available on MCA?"

Let me know if you want the LangChain + Ollama version too!


🔐 Note:

  • MCA's site doesn’t have an official API, so this is browser automation (Selenium).

  • Always follow the MCA's terms of use.


Response : for company Name entered as TESLA

🧑‍💼 Enter company name to check availability: TESLA

 To check the availability of a company name such as "TESLA" in India, you would need to use the Ministry of Corporate Affairs (MCA) portal. However, as a responsive AI model, I don't have real-time capabilities or access to external databases like the MCA portal. Here's what you can do:


1. Visit the official MCA portal at https://mca.gov.in/

2. Navigate to the 'Incorporate a company' section, often found under the 'Services' tab.

3. Look for the option to check name availability and click on it. You might need to register yourself if you haven't already.

4. Enter the proposed name "TESLA" (without quotes) in the provided field and follow the instructions to find out its availability.


Keep in mind that company names in India are not permitted to be identical or too similar to an existing name, or offensive, or potentially misleading. Also, certain words require government approval. For instance, 'Tesla' is a foreign word and might need additional approvals if it's the main component of your proposed company name.


If the name is available, you can proceed with the incorporation process, which involves preparing and submitting various documents and paying the required fees. If the name is not available, you will have to choose another name.


To stay updated on India's business registration process, visit the MCA portal or consult a professional service provider for guidance.




DevTools listening on ws://127.0.0.1:49360/devtools/browser/ab054f98-e916-4f94-9637-74557b3b00c9

Traceback (most recent call last):

  File "d:\demo\caai-ai\mcp-agentforcompanyname\llm_agent.py", line 34, in <module>

    main()

    ~~~~^^

  File "d:\demo\caai-ai\mcp-agentforcompanyname\llm_agent.py", line 30, in main

    result = check_company_name_availability(user_input)

  File "d:\demo\caai-ai\mcp-agentforcompanyname\mca_agent.py", line 16, in check_company_name_availability

    input_box = driver.find_element(By.ID, "companyName")

  File "C:\Users\AURMC\AppData\Roaming\Python\Python313\site-packages\selenium\webdriver\remote\webdriver.py", line 898, in find_element

    return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]

           ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  File "C:\Users\AURMC\AppData\Roaming\Python\Python313\site-packages\selenium\webdriver\remote\webdriver.py", line 429, in execute

    self.error_handler.check_response(response)

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^

  File "C:\Users\AURMC\AppData\Roaming\Python\Python313\site-packages\selenium\webdriver\remote\errorhandler.py", line 232, in check_response

    raise exception_class(message, screen, stacktrace)

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="companyName"]"}   

  (Session info: chrome=135.0.7049.115); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception

Stacktrace:

        GetHandleVerifier [0x00007FF70D23EFA5+77893]

        GetHandleVerifier [0x00007FF70D23F000+77984]

        (No symbol) [0x00007FF70D0091BA]

        (No symbol) [0x00007FF70D05F16D]

        (No symbol) [0x00007FF70D05F41C]

        (No symbol) [0x00007FF70D0B2237]

        (No symbol) [0x00007FF70D08716F]

        (No symbol) [0x00007FF70D0AF07F]

        (No symbol) [0x00007FF70D086F03]

        (No symbol) [0x00007FF70D050328]

        (No symbol) [0x00007FF70D051093]

        GetHandleVerifier [0x00007FF70D4F7B6D+2931725]

        GetHandleVerifier [0x00007FF70D4F2132+2908626]

        GetHandleVerifier [0x00007FF70D5100F3+3031443]

        GetHandleVerifier [0x00007FF70D2591EA+184970]

        GetHandleVerifier [0x00007FF70D26086F+215311]

        GetHandleVerifier [0x00007FF70D246EC4+110436]

        GetHandleVerifier [0x00007FF70D247072+110866]

        GetHandleVerifier [0x00007FF70D22D479+5401]

        BaseThreadInitThunk [0x00007FFC2732259D+29]

        RtlUserThreadStart [0x00007FFC287CAF38+40]


PS D:\demo\caai-ai\mcp-agentforcompanyname>

-----------------------------------------------------------------------------------------------------------------------

Any way Still, trying to figure out?!



Friday, March 21, 2025

#1 K8S Intro -Lab


GCP Kubernetes Hands-on Lab

Objective

By the end of this lab, students will be able to:

  • Log in to Google Cloud Platform (GCP)
  • Create a Kubernetes cluster using Google Kubernetes Engine (GKE)
  • Deploy and manage nodes
  • Deploy and manage pods
  • Run and expose an application

Prerequisites

  • A Google Cloud Platform (GCP) account
  • Billing enabled for the GCP project
  • Google Cloud SDK installed (or use Google Cloud Shell)
  • Basic understanding of Kubernetes concepts

Step 1: Log in to GCP

  1. Open Google Cloud Console.
  2. Click on Select a project and create a new project (or use an existing one).
  3. Enable the Kubernetes Engine API:
    • Navigate to APIs & Services > Library.
    • Search for Kubernetes Engine API and enable it.
  4. Open Cloud Shell (recommended) or install and configure gcloud CLI:
    gcloud auth login
    gcloud config set project [PROJECT_ID]
    




Step 2: Create a Kubernetes Cluster

  1. In the Google Cloud Console, navigate to the left panel and select Kubernetes Engine.
  2. Click on Create and choose either Auto Pilot or Standard (configure as needed).
  3. Select Zonal as the location type.
  4. Use the Release Channel (default) setting.
  5. (Optional) Use the Setup Guide from the left menu for guided setup.
  6. In the Default-pool section, configure:
    • Number of Nodes (e.g., 3)
    • Machine Configuration:
      • Nodes: E2
      • E2-micro instance type
  7. Click Create Cluster.
  8. Configure kubectl to use the cluster:
    gcloud container clusters get-credentials my-cluster --zone us-central1-a
    
  9. Verify the cluster status:
    kubectl get nodes
    

Step 3: Deploy a Pod

  1. Create a deployment running an Nginx container:
    kubectl create deployment niginx --image=nginx
    
  2. Check the status of the pod:
    kubectl get pods
    
    Output should show running if successful.

Step 4: Expose the Deployment

  1. Expose the deployment as a service:
    kubectl expose deployment my-nginx --type=LoadBalancer --port=80
    
  2. Get the external IP of the service:
    kubectl get services
    
  3. Open a web browser and enter the external IP to verify that Nginx is running.

Step 5: Scale the Deployment

  1. Scale the deployment to 3 replicas:
    kubectl scale deployment my-nginx --replicas=3
    
  2. Verify the number of pods:
    kubectl get pods
    

Step 6: Clean Up

  1. Delete the service:
    kubectl delete service my-nginx
    
  2. Delete the deployment:
    kubectl delete deployment my-nginx
    
  3. Delete the cluster:
    gcloud container clusters delete my-cluster --zone us-central1-a
    

    Note: If you do not delete the cluster and pods, you may continue to incur charges while they are running.


Additional Resources


Conclusion

In this lab, students learned how to create a Kubernetes cluster on GCP, deploy an application, expose it to the internet, scale it, and clean up resources. This provides a basic understanding of Kubernetes operations on Google Cloud.

Thursday, March 20, 2025

For Demo - Discussion?

25.03.2025

Let me explain how to create docker conatiner to run Document processing Agent by AI-Team :

Step 1:  

In Visual code editor create Agent-Doc folder

Please generate all the necessary files one by one:

  1. Agent.py - The Flask backend with Llama3.2 integration, EasyOCR document processing, and AI-based name suggestions.

  2. index.html - The frontend for user interaction with AI-generated data and document processing.

  3. Dockerfile - A Dockerfile to containerize the application.

  4. requirements.txt - Dependencies for the Flask server.

Let us generate these now.

Agent.py (backend)

from flask import Flask, jsonify, request
from flask_cors import CORS  # Import CORS
import requests
from functools import lru_cache
import asyncio
import easyocr
from PIL import Image
import io
import re
 

app = Flask(__name__)
CORS(app)  # Enable CORS globally for all routes

reader = easyocr.Reader(['en'])

# Mock database (replace with real database later)
mock_user_data = {
    "userId": 1,
    "panNumber": "ABCDE1234F",
    "registeredAddress": "123 Business Park, Mumbai, Maharashtra, India"
}

# Cache LLM responses for frequently asked questions
@lru_cache(maxsize=128)
def get_llm_response(prompt):
    full_prompt = f"{prompt} Please provide a short and relevant answer (max 10 words)."
    url = "http://localhost:11434/api/generate"
    payload = {
        "model": "llama3.2",
        "prompt": full_prompt,
        "stream": False
    }
    response = requests.post(url, json=payload)
    if response.status_code == 200:
        llm_response = response.json().get("response", "I am not sure about this.")
        if len(llm_response.split()) > 10:  # Limit to 10 words
            llm_response = " ".join(llm_response.split()[:10]) + "..."
        return llm_response
    return "Error communicating with LLM."

# Endpoint for basic and generic data
@app.route('/api/auto-fill', methods=['GET'])
async def auto_fill():
    user_data = mock_user_data

    business_activity_prompt = "Describe the primary business activity for a company in India."
    ownership_type_prompt = "What are common ownership types for Indian companies?"

    business_activity_task = asyncio.to_thread(get_llm_response, business_activity_prompt)
    ownership_type_task = asyncio.to_thread(get_llm_response, ownership_type_prompt)

    business_activity, ownership_type = await asyncio.gather(business_activity_task, ownership_type_task)

    return jsonify({
        "panNumber": user_data["panNumber"],
        "registeredAddress": user_data["registeredAddress"],
        "businessActivity": business_activity,
        "ownershipType": ownership_type
    })


# Other endpoints remain unchanged...
# Initialize EasyOCR reader
# English language

# Endpoint for document processing


@app.route('/api/process-documents', methods=['POST'])
def process_documents():
    try:
        print("Processing uploaded documents...")  # Log the start of the process

        files = request.files.getlist('documents')
        if not files:
            return jsonify({"error": "No files uploaded"}), 400

        extracted_data = {"gstNumber": "", "directorAadhaar": ""}

        for file in files:
            print(f"Processing file: {file.filename}")  # Log the file name

            # Check file type
            if file.filename.lower().endswith(('.png', '.jpg', '.jpeg')):
                print("Detected image file.")  # Log file type
                try:
                    image = Image.open(io.BytesIO(file.read()))
                    # Convert image to bytes for EasyOCR
                    image_bytes = io.BytesIO()
                    image.save(image_bytes, format='PNG')
                    image_bytes.seek(0)

                    # Extract text using EasyOCR
                    result = reader.readtext(image_bytes.getvalue())
                    text = " ".join([item[1] for item in result])  # Join all detected text
                    print(f"Extracted text from image: {text[:100]}...")  # Log extracted text
                except Exception as e:
                    print(f"Error processing image: {e}")
                    return jsonify({"error": f"Failed to process image file: {file.filename}"}), 500

            elif file.filename.lower().endswith('.pdf'):
                print("Detected PDF file.")  # Log file type
                try:
                    from pdf2image import convert_from_bytes
                    images = convert_from_bytes(file.read())
                    text = ""
                    for img in images:
                        # Convert each page to bytes for EasyOCR
                        img_byte_arr = io.BytesIO()
                        img.save(img_byte_arr, format='PNG')
                        img_byte_arr.seek(0)

                        # Extract text using EasyOCR
                        result = reader.readtext(img_byte_arr.getvalue())
                        text += " ".join([item[1] for item in result])  # Join all detected text
                    print(f"Extracted text from PDF: {text[:100]}...")  # Log extracted text
                except Exception as e:
                    print(f"Error processing PDF: {e}")
                    return jsonify({"error": f"Failed to process PDF file: {file.filename}"}), 500

            else:
                return jsonify({"error": f"Unsupported file type: {file.filename}"}), 400

            # Extract GST and Aadhaar numbers using regex
            gst_match = re.search(r'\b\d{2}[A-Z]{5}\d{4}[A-Z]{1}[A-Z\d]{1}[Z]{1}[A-Z\d]{1}\b', text)
            aadhaar_match = re.search(r'\b\d{4}-\d{4}-\d{4}\b', text)

            if gst_match:
                extracted_data["gstNumber"] = gst_match.group(0)
                print(f"Extracted GST Number: {gst_match.group(0)}")  # Log GST number
            if aadhaar_match:
                extracted_data["directorAadhaar"] = aadhaar_match.group(0)
                print(f"Extracted Aadhaar Number: {aadhaar_match.group(0)}")  # Log Aadhaar number

        return jsonify(extracted_data)

    except Exception as e:
        print(f"Unexpected error: {e}")  # Log unexpected errors
        return jsonify({"error": "An unexpected error occurred while processing the documents."}), 500


# Mock function to validate company names against Indian naming conventions
def validate_company_name(name):
    prohibited_words = ["bank", "government", "reserve"]  # Example prohibited words
    if any(word in name.lower() for word in prohibited_words):
        return False
    return True


# Function to generate AI-based name suggestions
def get_ai_suggestions(firstName):
    # Craft a prompt for the LLM
    prompt = f"Generate 5 creative and unique company name suggestions based on '{firstName}' that adhere to Indian company naming conventions."

    # Call the LLM via Ollama
    url = "http://localhost:11434/api/generate"
    payload = {
        "model": "llama3.2",
        "prompt": prompt,
        "stream": False
    }
    response = requests.post(url, json=payload)
    if response.status_code == 200:
        llm_response = response.json().get("response", "")
        suggestions = [name.strip() for name in llm_response.split("\n") if name.strip()]
        return suggestions[:5]  # Return only 5 suggestions
    return []


# Endpoint for AI-based name suggestions
@app.route('/api/suggest-names', methods=['POST'])
def suggest_names():
    try:
        data = request.get_json()
        firstName = data.get("firstName", "").strip()

        if not firstName:
            return jsonify({"error": "First name preference is required."}), 400

        # Validate the first preference
        if not validate_company_name(firstName):
            return jsonify({"error": "The provided name does not meet Indian company naming conventions."}), 400

        # Generate AI-based suggestions
        suggestions = get_ai_suggestions(firstName)

        if not suggestions:
            return jsonify({"error": "Failed to generate name suggestions."}), 500

        return jsonify({"suggestions": suggestions})

    except Exception as e:
        print(f"Unexpected error: {e}")
        return jsonify({"error": "An unexpected error occurred while generating name suggestions."}), 500


if __name__ == '__main__':
    app.run(debug=True, port=3000)

Now, let's generate the remaining files:

index.html (Frontend)

This file provides a simple interface to interact with the backend.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AI-Powered Form Generator</title>
</head>
<body>
    <h2>Generate Name Suggestions</h2>
    <input type="text" id="prompt" placeholder="Enter your prompt">
    <button onclick="generateText()">Generate</button>
    <p id="generatedResponse"></p>

    <h2>Process Document</h2>
    <input type="file" id="fileInput">
    <button onclick="processDoc()">Upload & Process</button>
    <p id="processedText"></p>

    <script>
        async function generateText() {
            let prompt = document.getElementById("prompt").value;
            let response = await fetch("/generate", {
                method: "POST",
                headers: {"Content-Type": "application/json"},
                body: JSON.stringify({ prompt })
            });
            let data = await response.json();
            document.getElementById("generatedResponse").innerText = data.generated_text;
        }

        async function processDoc() {
            let fileInput = document.getElementById("fileInput").files[0];
            let formData = new FormData();
            formData.append("file", fileInput);

            let response = await fetch("/process-doc", {
                method: "POST",
                body: formData
            });
            let data = await response.json();
            document.getElementById("processedText").innerText = data.text.join(", ");
        }
    </script>
</body>
</html>

Dockerfile

This file creates a containerized environment for the Flask application.

# Use official Python image as a base
FROM python:3.10

# Set working directory
WORKDIR /app

# Copy project files
COPY . .

# Install dependencies
RUN pip install -r requirements.txt

# Expose Flask port
EXPOSE 5000

# Run the Flask app
CMD ["python", "Agent.py"]

requirements.txt

This file lists all dependencies.

Flask
easyocr
ollama

Please install ollama from https://ollama.com/

Please pull llama3.2 from ollama by : 

ollama run llama3.2

Now you have everything to run your AI-powered form generator and document processor inside a Docker container! Let me know if you need any modifications. 🚀 

Friday, March 14, 2025

Run OLLAMA in Co-Lab

 

How to Run Any LLM Models in Google Colab

Introduction

Google Colab provides an easy and efficient way to run large language models (LLMs) without needing powerful local hardware. In this guide, we’ll walk through the steps to set up and run LLM models in Colab using Ollama.

Setting Up Google Colab

Step 1: Create a New Notebook

  1. Go to Google Colab.
  2. Create a new notebook and name it First.ipynb.
  3. Connect to a GPU instance.

Step 2: Enable GPU

  1. Click on Runtime in the menu.
  2. Select Change Runtime Type.
  3. Choose T4 GPU under hardware accelerator.
  4. Save and hit Connect.

To confirm GPU availability, check system resources. You should see around 15GB of VRAM.

!nvidia-smi

Installing Dependencies

Step 3: Install colab-xterm

To enable terminal access in Colab:

!pip install colab-xterm
%load_ext colabxterm
%xterm

Step 4: Install and Set Up Ollama

Ollama is a tool that allows you to run LLMs easily. Install it with the following command:

curl https://ollama.ai/install.sh | sh

Step 5: Start Ollama and Download a Model

ollama serve &
ollama pull llama3.2

You can verify the installation and downloaded models using:

ollama list
ollama show llama3.2

Step 6: Install the Ollama Python Package

!pip install ollama

Running LLM Models in Colab

Now, let's use Python to interact with the LLM model.

import ollama

prompt = "What is a pandas DataFrame?"

response = ollama.chat(
    model="llama3.2",
    messages=[{"role": "user", "content": prompt}]
)

print(response['message']['content'])

Conclusion

By following these steps, you can easily set up and run LLM models in Google Colab using Ollama. This method allows you to leverage cloud computing resources for AI model inference, making it accessible even on low-end devices.

Happy coding!

Ready-made Bash script for Docker workflow

R eady-made Bash script that automates your Docker workflow end-to-end: Build backend & frontend images Tag & push them to D...