Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| from PIL import Image | |
| import pytesseract | |
| import cv2 | |
| import numpy as np | |
| from transformers import pipeline | |
| import os | |
| print("Files in current directory:", os.listdir()) | |
| import csv | |
| # Set transformers cache to writable directory | |
| os.environ["TRANSFORMERS_CACHE"] = "/app/.cache/huggingface" | |
| # Load the LLM pipeline (using a smaller model for easier local use) | |
| llm = pipeline( | |
| "text-generation", | |
| model="tiiuae/falcon-rw-1b", | |
| tokenizer="tiiuae/falcon-rw-1b", | |
| max_new_tokens=300 | |
| ) | |
| # Function to parse CSV and format plans as text using your CSV headers | |
| def format_singtel_plans_from_csv(csv_path): | |
| formatted_plans = [] | |
| with open(csv_path, 'r', encoding='utf-8') as file: | |
| reader = csv.DictReader(file) | |
| for row in reader: | |
| plan_str = f"- {row['Plan Name']}: {row['Badge']}, {row['Price']}, {row['Features']}" | |
| formatted_plans.append(plan_str) | |
| return "\n".join(formatted_plans) | |
| # Prepare the plan text once at startup | |
| csv_path = os.path.join(os.path.dirname(__file__), "final_singtel_postpaid_mobile_plans.csv") | |
| plan_text = format_singtel_plans_from_csv(csv_path) | |
| def ocr_extract_and_recommend(image): | |
| if image is None: | |
| return "Please upload an image." | |
| # OCR preprocessing | |
| image_np = np.array(image) | |
| image_cv = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR) | |
| gray = cv2.cvtColor(image_cv, cv2.COLOR_BGR2GRAY) | |
| _, thresh = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY) | |
| # Extract text using pytesseract | |
| text = pytesseract.image_to_string(thresh).strip() | |
| if not text: | |
| return "Couldn't extract any text. Please upload a clearer image." | |
| # Construct prompt for LLM | |
| prompt = f""" | |
| You are a telco advisor helping users compare mobile plans. | |
| The user's current mobile bill contains: | |
| {text} | |
| Available Singtel mobile plans: | |
| {plan_text} | |
| Recommend the best Singtel plan (if applicable) and explain why it is better in terms of price, data, and features. | |
| """ | |
| # Get recommendation from LLM | |
| try: | |
| response = llm(prompt)[0]["generated_text"] | |
| except Exception as e: | |
| response = f"LLM generation failed: {str(e)}" | |
| return f"๐ **Extracted Bill Info:**\n{text}\n\n๐ข **Recommendation:**\n{response}" | |
| # Gradio interface | |
| iface = gr.Interface( | |
| fn=ocr_extract_and_recommend, | |
| inputs=gr.Image(type="pil", label="Upload your bill"), | |
| outputs="text", | |
| title="Singtel Plan Recommender with OCR", | |
| description="Upload your telco bill image to extract plan details and get personalized Singtel plan recommendations." | |
| ) | |
| if __name__ == "__main__": | |
| iface.launch(server_name="0.0.0.0", server_port=7860) | |