namanraj commited on
Commit
4d757af
·
1 Parent(s): 7bf9202

Remove nested duplicate folders and pycache

Browse files
app/__pycache__/__init__.cpython-310.pyc DELETED
Binary file (165 Bytes)
 
app/__pycache__/agent.cpython-310.pyc DELETED
Binary file (974 Bytes)
 
app/__pycache__/main.cpython-310.pyc DELETED
Binary file (1.54 kB)
 
app/app/__init__.py DELETED
@@ -1 +0,0 @@
1
-
 
 
app/app/__pycache__/__init__.cpython-310.pyc DELETED
Binary file (165 Bytes)
 
app/app/__pycache__/agent.cpython-310.pyc DELETED
Binary file (974 Bytes)
 
app/app/__pycache__/main.cpython-310.pyc DELETED
Binary file (1.54 kB)
 
app/app/agent.py DELETED
@@ -1,33 +0,0 @@
1
- from tools.ocr import run_ocr
2
- from tools.web_search import fetch_book_summary
3
- from tools.summarizer import summarize_page
4
- from tools.prompt_generator import generate_image_prompt
5
- from tools.image_gen import generate_image
6
- from evaluation.evaluation import evaluate_summary
7
-
8
- def run_agent(image_path: str, book_name: str, author_name: str = ""):
9
- ocr_text, confidence = run_ocr(image_path)
10
-
11
- book_summary = fetch_book_summary(book_name, author_name)
12
-
13
- page_summary = summarize_page(ocr_text)
14
-
15
- # Evaluate the summary for faithfulness and hallucination
16
- evaluation = evaluate_summary(ocr_text, page_summary)
17
-
18
- image_prompt = generate_image_prompt(
19
- page_summary=page_summary,
20
- book_context=book_summary
21
- )
22
-
23
- image = generate_image(image_prompt)
24
-
25
- return {
26
- "ocr_text": ocr_text,
27
- "ocr_confidence": confidence,
28
- "book_context": book_summary,
29
- "summary": page_summary,
30
- "image_prompt": image_prompt,
31
- "image": image,
32
- "evaluation": evaluation
33
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/app/main.py DELETED
@@ -1,53 +0,0 @@
1
- from fastapi import FastAPI, UploadFile
2
- from fastapi.middleware.cors import CORSMiddleware
3
- import shutil
4
- from app.agent import run_agent
5
-
6
- app = FastAPI()
7
-
8
- # Enable CORS for Streamlit Cloud
9
- app.add_middleware(
10
- CORSMiddleware,
11
- allow_origins=["*"], # Allows all origins
12
- allow_credentials=True,
13
- allow_methods=["*"], # Allows all methods
14
- allow_headers=["*"], # Allows all headers
15
- )
16
-
17
- @app.post("/process-page/")
18
- async def process_page(
19
- book_name: str,
20
- file: UploadFile,
21
- author_name: str = ""
22
- ):
23
- import tempfile
24
- import os
25
- import traceback
26
- from fastapi.responses import JSONResponse
27
-
28
- try:
29
- # Create a temporary file
30
- with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(file.filename)[1]) as tmp:
31
- shutil.copyfileobj(file.file, tmp)
32
- image_path = tmp.name
33
-
34
- import base64
35
-
36
- result = run_agent(image_path, book_name, author_name)
37
-
38
- image_b64 = ""
39
- if result["image"]:
40
- image_b64 = base64.b64encode(result["image"]).decode("utf-8")
41
-
42
- return {
43
- "ocr_text": result["ocr_text"],
44
- "ocr_confidence": result["ocr_confidence"],
45
- "book_context": result["book_context"],
46
- "summary": result["summary"],
47
- "image_prompt": result["image_prompt"],
48
- "image": image_b64
49
- }
50
- except Exception as e:
51
- error_msg = f"Server Error: {str(e)}\n{traceback.format_exc()}"
52
- print(error_msg)
53
- return JSONResponse(status_code=500, content={"detail": error_msg})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/app/schema.py DELETED
@@ -1,20 +0,0 @@
1
- from pydantic import BaseModel
2
- from typing import List
3
-
4
- class OCRResult(BaseModel):
5
- text: str
6
- confidence: float
7
-
8
- class PageSummary(BaseModel):
9
- summary: str
10
- key_entities: List[str]
11
- emotions: List[str]
12
-
13
- class ImagePrompt(BaseModel):
14
- prompt: str
15
- style: str
16
- mood: str
17
-
18
- class EvaluationResult(BaseModel):
19
- faithfulness_score: int
20
- hallucination: bool
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
evaluation/__pycache__/__init__.cpython-310.pyc DELETED
Binary file (172 Bytes)
 
evaluation/__pycache__/evaluation.cpython-310.pyc DELETED
Binary file (2.07 kB)
 
evaluation/evaluation/__init__.py DELETED
@@ -1 +0,0 @@
1
- # Evaluation module
 
 
evaluation/evaluation/__pycache__/__init__.cpython-310.pyc DELETED
Binary file (172 Bytes)
 
evaluation/evaluation/__pycache__/evaluation.cpython-310.pyc DELETED
Binary file (2.07 kB)
 
evaluation/evaluation/evaluation.py DELETED
@@ -1,72 +0,0 @@
1
- from huggingface_hub import InferenceClient
2
- import os
3
- import json
4
- from dotenv import load_dotenv
5
-
6
- load_dotenv()
7
-
8
- HF_API_KEY = os.getenv("HF_API_KEY")
9
- client = InferenceClient(token=HF_API_KEY)
10
-
11
-
12
- def evaluate_summary(ocr_text: str, summary: str) -> dict:
13
- """
14
- Evaluate the faithfulness of a summary against the original OCR text.
15
- Returns a dict with faithfulness_score (1-5) and hallucination (bool).
16
- """
17
- prompt = f"""You are an evaluation assistant. Compare the original OCR text with the generated summary.
18
-
19
- ORIGINAL OCR TEXT:
20
- {ocr_text}
21
-
22
- GENERATED SUMMARY:
23
- {summary}
24
-
25
- Evaluate:
26
- 1. Faithfulness Score (1-5): How accurately does the summary reflect the original text?
27
- - 5: Perfect, all details are accurate
28
- - 4: Very good, minor omissions
29
- - 3: Acceptable, some details missing or slightly off
30
- - 2: Poor, significant inaccuracies
31
- - 1: Very poor, mostly inaccurate
32
-
33
- 2. Hallucination: Does the summary contain information NOT present in the original text?
34
-
35
- Respond ONLY with valid JSON in this exact format:
36
- {{"faithfulness_score": <int 1-5>, "hallucination": <true/false>}}"""
37
-
38
- try:
39
- response = client.chat_completion(
40
- messages=[
41
- {
42
- "role": "user",
43
- "content": prompt
44
- }
45
- ],
46
- model="HuggingFaceH4/zephyr-7b-beta",
47
- max_tokens=100,
48
- temperature=0.1
49
- )
50
-
51
- result_text = response.choices[0].message.content.strip()
52
-
53
- # Try to parse JSON from the response
54
- try:
55
- # Find JSON in the response
56
- start = result_text.find('{')
57
- end = result_text.rfind('}') + 1
58
- if start != -1 and end > start:
59
- result = json.loads(result_text[start:end])
60
- return {
61
- "faithfulness_score": result.get("faithfulness_score", 3),
62
- "hallucination": result.get("hallucination", False)
63
- }
64
- except json.JSONDecodeError:
65
- pass
66
-
67
- # Default fallback
68
- return {"faithfulness_score": 3, "hallucination": False}
69
-
70
- except Exception as e:
71
- print(f"Evaluation error: {e}")
72
- return {"faithfulness_score": 0, "hallucination": False, "error": str(e)}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
tools/__pycache__/image_gen.cpython-310.pyc DELETED
Binary file (910 Bytes)
 
tools/__pycache__/ocr.cpython-310.pyc DELETED
Binary file (1.21 kB)
 
tools/__pycache__/prompt_generator.cpython-310.pyc DELETED
Binary file (4.41 kB)
 
tools/__pycache__/summarizer.cpython-310.pyc DELETED
Binary file (2 kB)
 
tools/__pycache__/web_search.cpython-310.pyc DELETED
Binary file (2.12 kB)
 
tools/tools/__pycache__/image_gen.cpython-310.pyc DELETED
Binary file (910 Bytes)
 
tools/tools/__pycache__/ocr.cpython-310.pyc DELETED
Binary file (1.21 kB)
 
tools/tools/__pycache__/prompt_generator.cpython-310.pyc DELETED
Binary file (4.41 kB)
 
tools/tools/__pycache__/summarizer.cpython-310.pyc DELETED
Binary file (2 kB)
 
tools/tools/__pycache__/web_search.cpython-310.pyc DELETED
Binary file (2.12 kB)
 
tools/tools/image_gen.py DELETED
@@ -1,30 +0,0 @@
1
- from huggingface_hub import InferenceClient
2
- import os
3
- from dotenv import load_dotenv
4
-
5
- load_dotenv()
6
-
7
- HF_API_KEY = os.getenv("HF_API_KEY")
8
- HF_MODEL = "stabilityai/stable-diffusion-xl-base-1.0"
9
-
10
- def generate_image(prompt: str):
11
- """Use HuggingFace Hub InferenceClient for image generation"""
12
-
13
- client = InferenceClient(token=HF_API_KEY)
14
-
15
- try:
16
- # Generate image using text-to-image
17
- image = client.text_to_image(
18
- prompt,
19
- model=HF_MODEL
20
- )
21
-
22
- # Convert PIL Image to bytes
23
- from io import BytesIO
24
- img_byte_arr = BytesIO()
25
- image.save(img_byte_arr, format='PNG')
26
- return img_byte_arr.getvalue()
27
-
28
- except Exception as e:
29
- print(f"Image generation error: {str(e)}")
30
- return b"" # Return empty bytes on error
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
tools/tools/ocr.py DELETED
@@ -1,40 +0,0 @@
1
- import cv2
2
- import pytesseract
3
- import os
4
- import shutil
5
-
6
- # Check for TESSERACT_PATH env var, else default
7
- tesseract_cmd = os.getenv("TESSERACT_PATH", r"C:\Program Files\Tesseract-OCR\tesseract.exe")
8
- if not os.path.exists(tesseract_cmd):
9
- # Try to find in PATH
10
- tesseract_cmd_shutil = shutil.which("tesseract")
11
- if tesseract_cmd_shutil:
12
- tesseract_cmd = tesseract_cmd_shutil
13
- else:
14
- print(f"Warning: Tesseract not found at {tesseract_cmd}. OCR may fail.")
15
-
16
- pytesseract.pytesseract.tesseract_cmd = tesseract_cmd
17
-
18
- def run_ocr(image_path: str):
19
- img = cv2.imread(image_path)
20
- gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
21
-
22
- data = pytesseract.image_to_data(
23
- gray, output_type=pytesseract.Output.DICT
24
- )
25
-
26
- text = " ".join([t for t in data["text"] if t.strip()])
27
-
28
- # Filter valid confidence values (tesseract returns -1 for invalid)
29
- confs = []
30
- for c in data["conf"]:
31
- try:
32
- val = int(c)
33
- if val >= 0:
34
- confs.append(val)
35
- except (ValueError, TypeError):
36
- pass
37
-
38
- confidence = sum(confs) / len(confs) / 100 if confs else 0.0
39
-
40
- return text.strip(), confidence
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
tools/tools/prompt_generator.py DELETED
@@ -1,144 +0,0 @@
1
- from huggingface_hub import InferenceClient
2
- import os
3
- from dotenv import load_dotenv
4
-
5
- load_dotenv()
6
-
7
- HF_API_KEY = os.getenv("HF_API_KEY")
8
- client = InferenceClient(token=HF_API_KEY)
9
-
10
-
11
- def extract_book_metadata(book_context: str) -> dict:
12
- """Extract structured metadata from Open Library context."""
13
- metadata = {
14
- "title": "",
15
- "author": "",
16
- "year": "",
17
- "genre": "",
18
- "subjects": ""
19
- }
20
-
21
- if not book_context:
22
- return metadata
23
-
24
- for line in book_context.split("\n"):
25
- if line.startswith("Title:"):
26
- metadata["title"] = line.replace("Title:", "").strip()
27
- elif line.startswith("Author:"):
28
- metadata["author"] = line.replace("Author:", "").strip()
29
- elif line.startswith("First Published:"):
30
- metadata["year"] = line.replace("First Published:", "").strip()
31
- elif line.startswith("Subjects:"):
32
- metadata["subjects"] = line.replace("Subjects:", "").strip()
33
- metadata["genre"] = metadata["subjects"].split(",")[0].strip()
34
-
35
- return metadata
36
-
37
-
38
- def get_era_style(year: str) -> str:
39
- """Map publication year to artistic era and style."""
40
- try:
41
- yr = int(year)
42
- if yr < 1800:
43
- return "classical painting style, baroque or renaissance aesthetics, rich oil painting textures"
44
- elif yr < 1850:
45
- return "romantic era illustration, dramatic landscapes, emotional intensity, JMW Turner inspired"
46
- elif yr < 1900:
47
- return "Victorian illustration style, detailed engravings, Pre-Raphaelite influences, realistic portraiture"
48
- elif yr < 1950:
49
- return "early 20th century illustration, art nouveau elements, golden age illustration style"
50
- elif yr < 2000:
51
- return "mid-century illustration, bold compositions, realistic rendering"
52
- else:
53
- return "contemporary digital art, cinematic composition, photorealistic elements"
54
- except:
55
- return "classical book illustration style"
56
-
57
-
58
- def refine_prompt_with_llm(scene_summary: str, book_context: str, metadata: dict) -> str:
59
- """Use LLM to create a refined, thematic prompt."""
60
-
61
- era_style = get_era_style(metadata.get("year", ""))
62
-
63
- try:
64
- response = client.chat_completion(
65
- messages=[
66
- {
67
- "role": "system",
68
- "content": """You are an expert art director creating image prompts for book illustrations.
69
- Your task is to convert a scene description into a detailed visual prompt that:
70
- 1. Preserves the literary theme and mood of the book
71
- 2. Uses period-appropriate visual style
72
- 3. Focuses on concrete visual elements (lighting, composition, colors)
73
- 4. Avoids inventing details not in the scene
74
-
75
- Output ONLY the refined prompt, no explanations."""
76
- },
77
- {
78
- "role": "user",
79
- "content": f"""Create an illustration prompt for this scene:
80
-
81
- BOOK: {metadata.get('title', 'Unknown')} by {metadata.get('author', 'Unknown')}
82
- ERA: {metadata.get('year', 'Unknown')}
83
- GENRE: {metadata.get('genre', 'Literary Fiction')}
84
- RECOMMENDED STYLE: {era_style}
85
-
86
- SCENE TO ILLUSTRATE:
87
- {scene_summary}
88
-
89
- Generate a detailed, visual prompt that captures the essence of this scene while staying true to the book's era and theme."""
90
- }
91
- ],
92
- model="HuggingFaceH4/zephyr-7b-beta",
93
- max_tokens=400,
94
- temperature=0.5
95
- )
96
- return response.choices[0].message.content
97
- except Exception as e:
98
- print(f"LLM refinement failed: {e}")
99
- return None
100
-
101
-
102
- def generate_image_prompt(page_summary: str, book_context: str) -> str:
103
- """
104
- Generate a refined, theme-preserving image prompt.
105
- Uses LLM to enhance the prompt with book-specific style.
106
- """
107
-
108
- # Extract metadata from book context
109
- metadata = extract_book_metadata(book_context)
110
-
111
- # Get era-appropriate style
112
- era_style = get_era_style(metadata.get("year", ""))
113
-
114
- # Try LLM refinement
115
- refined_prompt = refine_prompt_with_llm(page_summary, book_context, metadata)
116
-
117
- if refined_prompt:
118
- # Add quality modifiers to LLM output
119
- final_prompt = f"""masterpiece, best quality, highly detailed illustration
120
-
121
- {refined_prompt}
122
-
123
- STYLE: {era_style}
124
- QUALITY: professional book illustration, sharp details, rich textures"""
125
- else:
126
- # Fallback to template-based prompt
127
- final_prompt = f"""masterpiece, best quality, highly detailed illustration
128
-
129
- BOOK: {metadata.get('title', 'Unknown')} ({metadata.get('year', '')})
130
- GENRE: {metadata.get('genre', 'Literary Fiction')}
131
-
132
- SCENE:
133
- {page_summary}
134
-
135
- STYLE: {era_style}
136
- ATMOSPHERE: Faithful to the literary source, emotionally resonant
137
- QUALITY: professional book illustration, sharp details, rich textures"""
138
-
139
- return final_prompt.strip()
140
-
141
-
142
- def validate_prompt(prompt: str, page_summary: str) -> bool:
143
- """Validates prompt is correctly formatted."""
144
- return "SCENE" in prompt or "illustration" in prompt.lower()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
tools/tools/summarizer.py DELETED
@@ -1,59 +0,0 @@
1
- from huggingface_hub import InferenceClient
2
- import os
3
- from dotenv import load_dotenv
4
-
5
- load_dotenv()
6
-
7
- HF_API_KEY = os.getenv("HF_API_KEY")
8
-
9
- client = InferenceClient(token=HF_API_KEY)
10
-
11
- SYSTEM_PROMPT = """You are an expert literary analyst. Your task is to analyze book page text and extract key visual and narrative elements.
12
-
13
- You must respond in the following structured format:
14
-
15
- **SCENE DESCRIPTION**: A vivid 2-3 sentence description of what is happening in this passage.
16
-
17
- **CHARACTERS**: List any characters mentioned with brief descriptions (appearance, emotion, action).
18
-
19
- **SETTING**: Describe the physical location, time of day, weather, and atmosphere.
20
-
21
- **MOOD**: The emotional tone (e.g., tense, romantic, melancholic, adventurous).
22
-
23
- **KEY VISUAL ELEMENTS**: List 3-5 specific objects, colors, or visual details mentioned.
24
-
25
- **ACTION**: What is the main action or event occurring?
26
-
27
- Be specific and focus on visually representable details. If information is not available, make reasonable inferences based on context."""
28
-
29
- def summarize_page(ocr_text: str) -> str:
30
- """Extract structured visual elements from book page text"""
31
-
32
- if not ocr_text or len(ocr_text.strip()) < 20:
33
- return "Insufficient text extracted from the image."
34
-
35
- try:
36
- response = client.chat_completion(
37
- messages=[
38
- {
39
- "role": "system",
40
- "content": SYSTEM_PROMPT
41
- },
42
- {
43
- "role": "user",
44
- "content": f"""Analyze the following book page text and extract visual elements for illustration:
45
-
46
- ---
47
- {ocr_text}
48
- ---
49
-
50
- Provide your structured analysis:"""
51
- }
52
- ],
53
- model="HuggingFaceH4/zephyr-7b-beta",
54
- max_tokens=800,
55
- temperature=0.4
56
- )
57
- return response.choices[0].message.content
58
- except Exception as e:
59
- return f"Error during summarization: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
tools/tools/web_search.py DELETED
@@ -1,82 +0,0 @@
1
- import requests
2
- from urllib.parse import quote
3
-
4
- def fetch_book_summary(book_name: str, author_name: str = "") -> str:
5
- """
6
- Fetch book summary from Open Library API.
7
- Uses both book name and author for accurate results.
8
- """
9
-
10
- if not book_name or len(book_name.strip()) < 2:
11
- return ""
12
-
13
- # Build search query with author if provided
14
- search_query = book_name
15
- if author_name:
16
- search_query = f"{book_name} {author_name}"
17
-
18
- # Strategy 1: Open Library Search API
19
- try:
20
- search_url = "https://openlibrary.org/search.json"
21
- params = {
22
- "title": book_name,
23
- "limit": 1
24
- }
25
- if author_name:
26
- params["author"] = author_name
27
-
28
- r = requests.get(search_url, params=params, timeout=10)
29
-
30
- if r.status_code == 200:
31
- data = r.json()
32
- docs = data.get("docs", [])
33
- if docs:
34
- book = docs[0]
35
- title = book.get("title", book_name)
36
- authors = ", ".join(book.get("author_name", ["Unknown"]))
37
- first_sentence = " ".join(book.get("first_sentence", [""]))
38
- subjects = ", ".join(book.get("subject", [])[:5])
39
- publish_year = book.get("first_publish_year", "Unknown")
40
-
41
- summary = f"Title: {title}\n"
42
- summary += f"Author: {authors}\n"
43
- summary += f"First Published: {publish_year}\n"
44
- if subjects:
45
- summary += f"Subjects: {subjects}\n"
46
- if first_sentence:
47
- summary += f"Opening: {first_sentence}\n"
48
-
49
- # Try to get description from work
50
- work_key = book.get("key", "")
51
- if work_key:
52
- try:
53
- work_url = f"https://openlibrary.org{work_key}.json"
54
- wr = requests.get(work_url, timeout=5)
55
- if wr.status_code == 200:
56
- work_data = wr.json()
57
- desc = work_data.get("description", "")
58
- if isinstance(desc, dict):
59
- desc = desc.get("value", "")
60
- if desc:
61
- summary += f"\nDescription: {desc[:500]}"
62
- except:
63
- pass
64
-
65
- return summary
66
- except Exception as e:
67
- print(f"Open Library failed: {e}")
68
-
69
- # Strategy 2: DuckDuckGo Instant Answers
70
- try:
71
- ddg_url = f"https://api.duckduckgo.com/?q={quote(search_query + ' book')}&format=json&no_html=1"
72
- r = requests.get(ddg_url, timeout=10)
73
-
74
- if r.status_code == 200:
75
- data = r.json()
76
- abstract = data.get("Abstract", "")
77
- if abstract:
78
- return f"DuckDuckGo: {abstract}"
79
- except Exception as e:
80
- print(f"DuckDuckGo failed: {e}")
81
-
82
- return f"No book information found for '{book_name}'" + (f" by {author_name}" if author_name else "")