Spaces:
Sleeping
Sleeping
Download llm.py from PixCell-SS/PixCell-Backend: direct link, hf CLI and curl.
- Browser
- Download file 3.02 kB
-
https://huggingface.co/spaces/PixCell-SS/PixCell-Backend/resolve/main/llm.py
- Command line
-
hf download hf://spaces/PixCell-SS/PixCell-Backend/llm.py
-
curl -L -o llm.py https://huggingface.co/spaces/PixCell-SS/PixCell-Backend/resolve/main/llm.py
3.02 kB
| import os | |
| import google.generativeai as genai | |
| from typing import Dict, Any | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| GEMINI_API_KEY = os.getenv("GEMINI_API_KEY") | |
| if not GEMINI_API_KEY: | |
| raise RuntimeError("GEMINI_API_KEY environment variable is not set. Please set it in your environment or .env file.") | |
| # Set a working US proxy (test and replace; this one is from free lists as of Dec 2025) | |
| # Find fresh ones at free-proxy-list.net or proxylister.com — filter for US, HTTP, high uptime | |
| #os.environ['HTTP_PROXY'] = 'http://198.199.86.11:80' | |
| #os.environ['HTTPS_PROXY'] = 'http://198.199.86.11:80' | |
| # Get API key from environment variable | |
| # Check GC LOL -molt | |
| def build_prompt_from_counts(class_counts: Dict[str, int], sample_type: str = "Blood smear", stain: str = "Giemsa", magnification: str = "1000x") -> str: | |
| """ | |
| Build a prompt for AI analysis based on detection results. | |
| Args: | |
| class_counts: Dictionary mapping class names to counts | |
| sample_type: Type of sample (default: "Blood smear") | |
| stain: Type of stain used (default: "Giemsa") | |
| magnification: Microscope magnification (default: "1000x") | |
| Returns: | |
| Formatted prompt string for AI analysis | |
| """ | |
| summary = "Detected features in sample:\n" | |
| for cls, count in class_counts.items(): | |
| summary += f"- {count} {cls.replace('_', ' ')}(s)\n" | |
| summary += f"Sample type: {sample_type}\nStain: {stain}\nMagnification: {magnification}\n" | |
| return f""" | |
| You are a medical pathologist. Based on the following detection results from a microscopic image, write a concise diagnostic summary and suggest next steps. | |
| {summary} | |
| Output: | |
| - Diagnostic Summary: | |
| - Recommendation: | |
| """ | |
| def analyze_detections(class_counts: Dict[str, int], sample_type: str = "Blood smear", stain: str = "Giemsa", magnification: str = "1000x") -> Dict[str, Any]: | |
| """ | |
| Analyze detection results using Gemini AI. | |
| Args: | |
| class_counts: Dictionary mapping class names to counts | |
| sample_type: Type of sample | |
| stain: Type of stain used | |
| magnification: Microscope magnification | |
| Returns: | |
| Dictionary containing analysis results | |
| """ | |
| try: | |
| # Configure the API key | |
| genai.configure(api_key=GEMINI_API_KEY,transport='rest') | |
| #print("Gemini configured with proxy successfully.") | |
| # Initialize the Gemini model | |
| model = genai.GenerativeModel('gemini-2.5-flash') | |
| # Build the prompt | |
| prompt = build_prompt_from_counts(class_counts, sample_type, stain, magnification) | |
| # Generate content from the model | |
| response = model.generate_content(prompt) | |
| return { | |
| "success": True, | |
| "analysis": response.text, | |
| "detection_summary": class_counts | |
| } | |
| except Exception as e: | |
| return { | |
| "success": False, | |
| "error": str(e), | |
| "detection_summary": class_counts | |
| } | |