Spaces:
Configuration error
Configuration error
Update app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364import gradio as grfrom huggingface_hub import InferenceClient"""For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference"""client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")def respond(message,history: list[tuple[str, str]],system_message,max_tokens,temperature,top_p,):messages = [{"role": "system", "content": system_message}]for val in history:if val[0]:messages.append({"role": "user", "content": val[0]})if val[1]:messages.append({"role": "assistant", "content": val[1]})messages.append({"role": "user", "content": message})response = ""for message in client.chat_completion(messages,max_tokens=max_tokens,stream=True,temperature=temperature,top_p=top_p,):token = message.choices[0].delta.contentresponse += tokenyield response"""For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface"""demo = gr.ChatInterface(respond,additional_inputs=[gr.Textbox(value="You are a friendly Chatbot.", label="System message"),gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),gr.Slider(minimum=0.1,maximum=1.0,value=0.95,step=0.05,label="Top-p (nucleus sampling)",),],)if __name__ == "__main__":demo.launch()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744# Complete Blood Report Analyzer for Hugging Face# ==============================================# Import Librariesimport osimport gradio as grimport google.generativeai as genaiimport fitz # PyMuPDFfrom PIL import Image, ImageEnhanceimport ioimport reimport jsonimport numpy as npimport pandas as pdfrom datetime import datetimeimport base64# Blood Report Analyzer Implementation# Configure Google Gemini APIdef configure_genai(api_key):genai.configure(api_key=api_key)# Use Gemini Pro Vision for image analysisvision_model = genai.GenerativeModel('gemini-pro-vision')# Use Gemini Pro for text analysis (better for structured text)text_model = genai.GenerativeModel('gemini-pro')return vision_model, text_model# Image preprocessing to improve OCRdef preprocess_image(image):# Convert to grayscaleimg_gray = image.convert('L')# Enhance contrastenhancer = ImageEnhance.Contrast(img_gray)img_enhanced = enhancer.enhance(2.0)# Increase sharpnesssharpness = ImageEnhance.Sharpness(img_enhanced)img_sharp = sharpness.enhance(2.0)return img_sharp# Extract text from PDF with advanced techniquesdef extract_text_from_pdf(pdf_file):doc = fitz.open(stream=pdf_file, filetype="pdf")complete_text = ""images = []tables = []for page_num in range(len(doc)):page = doc.load_page(page_num)# Get text with improved layout preservationtext = page.get_text("dict")blocks = text.get("blocks", [])# Process text blocks to preserve table-like structurespage_text = ""for block in blocks:if block.get("type") == 0: # Text blockfor line in block.get("lines", []):line_text = " ".join([span.get("text", "") for span in line.get("spans", [])])page_text += line_text + "\n"complete_text += page_text + "\n\n"# Extract tables using heuristics# Look for grid-like structures in the texttable_candidates = re.findall(r'(?:\w+[\t ]+){2,}(?:\d+\.?\d*[\t ]+){2,}', page_text)if table_candidates:tables.extend(table_candidates)# Extract images for visual analysisimage_list = page.get_images(full=True)for img_index, img in enumerate(image_list):xref = img[0]base_image = doc.extract_image(xref)image_bytes = base_image["image"]image = Image.open(io.BytesIO(image_bytes))# Only keep images that might be charts or reports# (filter out logos and decorative elements)if image.width > 200 and image.height > 200:# Preprocess image to improve readabilityprocessed_image = preprocess_image(image)images.append(processed_image)return complete_text, images, tables# Blood markers dictionary for referenceBLOOD_MARKERS = {"Vitamin D": ["25-OH Vitamin D", "Vitamin D, 25-Hydroxy", "25(OH)D", "Calcidiol"],"Vitamin B12": ["Cobalamin", "Cyanocobalamin", "Methylcobalamin", "B-12"],"Folate": ["Vitamin B9", "Folic Acid"],"Vitamin A": ["Retinol", "Beta-carotene"],"Vitamin E": ["Tocopherol", "Alpha-tocopherol"],"Vitamin K": ["Phylloquinone", "Menaquinone"],"Vitamin C": ["Ascorbic Acid", "L-ascorbic acid"],"Vitamin B1": ["Thiamine", "Thiamin"],"Vitamin B2": ["Riboflavin"],"Vitamin B3": ["Niacin", "Nicotinic acid"],"Vitamin B5": ["Pantothenic acid"],"Vitamin B6": ["Pyridoxine", "Pyridoxal", "Pyridoxamine"],"Vitamin B7": ["Biotin"],"Iron": ["Ferritin", "Transferrin", "TIBC", "UIBC", "Serum Iron"],"Calcium": ["Ca", "Serum Calcium", "Ionized Calcium"],"Magnesium": ["Mg", "Serum Magnesium"],"Zinc": ["Zn", "Serum Zinc"],"Selenium": ["Se", "Serum Selenium"],"Iodine": ["I", "Urinary Iodine"]}# Normal ranges reference (based on Indian standards)REFERENCE_RANGES = {"Vitamin D": {"unit": "ng/mL", "min": 30, "max": 100,"deficiency": "<20", "insufficiency": "20-29"},"Vitamin B12": {"unit": "pg/mL", "min": 211, "max": 911,"deficiency": "<200", "insufficiency": "200-300"},"Folate": {"unit": "ng/mL", "min": 5.9, "max": 24.8,"deficiency": "<5.9"},"Ferritin": {"unit": "ng/mL", "min_male": 30, "max_male": 400,"min_female": 13, "max_female": 150,"deficiency_male": "<30", "deficiency_female": "<13"},"Hemoglobin": {"unit": "g/dL","min_male": 13.5, "max_male": 17.5,"min_female": 12.0, "max_female": 15.5,"deficiency_male": "<13.5", "deficiency_female": "<12.0"},"Calcium": {"unit": "mg/dL", "min": 8.6, "max": 10.3,"deficiency": "<8.6"},"Magnesium": {"unit": "mg/dL", "min": 1.7, "max": 2.2,"deficiency": "<1.7"},"Zinc": {"unit": "ΞΌg/dL", "min": 70, "max": 120,"deficiency": "<70"}}# Extract blood markers and values from textdef extract_blood_markers(text):extracted_markers = {}# Iterate through all known markers and their aliasesfor vitamin, aliases in BLOOD_MARKERS.items():all_terms = aliases + [vitamin]for term in all_terms:# Look for the marker and its value# Pattern matches: Marker name: value unit# Or: Marker name value unitpattern = r'(?i)(%s)\s*[:=-]?\s*(\d+\.?\d*)' % re.escape(term)matches = re.findall(pattern, text)if matches:for match in matches:marker, value = match# Convert to float if possibletry:value = float(value)extracted_markers[vitamin] = valuebreak # Found a value for this vitamin, move to nextexcept ValueError:continuereturn extracted_markers# Analyze report with Gemini using structured approachdef analyze_report(vision_model, text_model, content, extracted_markers, is_text=False):# Create structured input for better analysisanalysis_prompt = f"""I need a detailed analysis of this blood test report. Focus specifically on vitamin, mineral and nutritional deficiencies.The report is from India, so provide recommendations relevant to Indian context, diet, and healthcare practices.For each identified deficiency:1. Specify the exact deficiency (vitamin/mineral name)2. Current level from report and normal reference range3. Severity (mild/moderate/severe)4. Recommended daily dosage in appropriate units (mg, mcg, IU) for supplementation5. Duration of recommended supplementation6. Specific health impacts this deficiency is causing or may cause7. Recommended foods available in India that address this deficiency (include both vegetarian and non-vegetarian options)8. Any additional blood tests that should be considered for confirmationAlso provide:- A comprehensive summary of all nutritional findings- Lifestyle modifications specific to Indian context- Any concerning values that require immediate medical attention- Follow-up testing recommendations with timelineIf you cannot confidently determine specific deficiencies, explain why and suggest further tests.The extracted markers I've identified include: {json.dumps(extracted_markers)}Format your response as structured JSON with the following schema:{{"deficiencies": [{{"nutrient": "string","current_level": "string","reference_range": "string","severity": "string","recommended_dosage": "string","supplementation_duration": "string","health_impacts": ["string"],"recommended_foods": {{"vegetarian": ["string"],"non_vegetarian": ["string"]}},"confirmation_tests": ["string"]}}],"summary": "string","lifestyle_modifications": ["string"],"urgent_concerns": ["string"] or null,"followup_recommendations": {{"tests": ["string"],"timeline": "string"}}}}"""try:if is_text:full_content = content + "\n\nExtracted markers: " + json.dumps(extracted_markers)response = text_model.generate_content([analysis_prompt, full_content])else:# For image, combine extracted markers with the imageresponse = vision_model.generate_content([analysis_prompt, content])# Extract JSON from responseresponse_text = response.text# Find JSON object in the responsejson_match = re.search(r'```json\s*([\s\S]*?)\s*```', response_text)if json_match:json_str = json_match.group(1)else:# Try to find JSON without code blocksjson_match = re.search(r'({[\s\S]*})', response_text)if json_match:json_str = json_match.group(1)else:return {"error": "Failed to parse JSON response", "raw_response": response_text}# Parse JSONtry:result = json.loads(json_str)return resultexcept json.JSONDecodeError:return {"error": "Invalid JSON response", "raw_response": response_text}except Exception as e:return {"error": f"Analysis failed: {str(e)}"}# Generate personalized recommendation reportdef generate_recommendation_html(analysis_result, patient_info=None):if "error" in analysis_result:return f"<div class='error'>Error in analysis: {analysis_result['error']}</div>"# Current date for the reportcurrent_date = datetime.now().strftime("%d %B, %Y")# Start building HTMLhtml = f"""<div style="font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; line-height: 1.6;"><div style="text-align: center; border-bottom: 2px solid #2c3e50; padding-bottom: 10px; margin-bottom: 20px;"><h1 style="color: #2c3e50;">Nutritional Analysis Report</h1><p>Generated on: {current_date}</p>{f"<p>Patient: {patient_info['name']} | Age: {patient_info['age']} | Gender: {patient_info['gender']}</p>" if patient_info else ""}</div><div style="background-color: #f9f9f9; border-left: 4px solid #3498db; padding: 15px; margin-bottom: 25px;"><h2 style="color: #3498db; margin-top: 0;">Summary</h2><p>{analysis_result.get('summary', 'No summary available')}</p></div>"""# Add deficiencies sectiondeficiencies = analysis_result.get('deficiencies', [])if deficiencies:html += '<h2 style="color: #2c3e50; border-bottom: 1px solid #ddd; padding-bottom: 8px;">Detected Deficiencies</h2>'for deficiency in deficiencies:severity_color = {"mild": "#f39c12","moderate": "#e67e22","severe": "#c0392b"}.get(deficiency.get('severity', '').lower(), "#7f8c8d")html += f"""<div style="margin-bottom: 30px; background-color: #f8f9fa; border-radius: 5px; padding: 15px; box-shadow: 0 1px 3px rgba(0,0,0,0.1);"><h3 style="color: {severity_color}; margin-top: 0;">{deficiency.get('nutrient', 'Unknown')}<span style="font-size: 0.8em; background-color: {severity_color}; color: white; padding: 3px 8px; border-radius: 3px; margin-left: 10px;">{deficiency.get('severity', 'Unknown')} deficiency</span></h3><div style="display: grid; grid-template-columns: 1fr 1fr; gap: 15px; margin-bottom: 15px;"><div><p><strong>Current Level:</strong> {deficiency.get('current_level', 'N/A')}</p><p><strong>Reference Range:</strong> {deficiency.get('reference_range', 'N/A')}</p><p><strong>Recommended Dosage:</strong> {deficiency.get('recommended_dosage', 'N/A')}</p><p><strong>Duration:</strong> {deficiency.get('supplementation_duration', 'N/A')}</p></div><div><p><strong>Health Impacts:</strong></p><ul style="margin-top: 5px; padding-left: 20px;">"""# Add health impactsfor impact in deficiency.get('health_impacts', ['N/A']):html += f"<li>{impact}</li>"html += """</ul></div></div><div style="margin-top: 15px;"><h4 style="color: #2c3e50; margin-bottom: 8px;">Recommended Foods</h4><div style="display: grid; grid-template-columns: 1fr 1fr; gap: 15px;"><div><h5 style="color: #27ae60; margin-bottom: 5px;">Vegetarian Options</h5><ul style="margin-top: 5px; padding-left: 20px;">"""# Add vegetarian foodsveg_foods = deficiency.get('recommended_foods', {}).get('vegetarian', ['N/A'])for food in veg_foods:html += f"<li>{food}</li>"html += """</ul></div><div><h5 style="color: #c0392b; margin-bottom: 5px;">Non-Vegetarian Options</h5><ul style="margin-top: 5px; padding-left: 20px;">"""# Add non-vegetarian foodsnon_veg_foods = deficiency.get('recommended_foods', {}).get('non_vegetarian', ['N/A'])for food in non_veg_foods:html += f"<li>{food}</li>"html += """</ul></div></div></div><div style="margin-top: 15px; background-color: #eaf2f8; padding: 10px; border-radius: 4px;"><h4 style="color: #2980b9; margin-top: 0; margin-bottom: 8px;">Additional Tests</h4><ul style="margin-top: 5px; padding-left: 20px;">"""# Add confirmation teststests = deficiency.get('confirmation_tests', ['None recommended'])for test in tests:html += f"<li>{test}</li>"html += """</ul></div></div>"""else:html += '<div style="padding: 15px; background-color: #e8f8f5; border-radius: 5px; margin-bottom: 25px;"><p>No specific deficiencies detected.</p></div>'# Add lifestyle modificationslifestyle = analysis_result.get('lifestyle_modifications', [])if lifestyle:html += """<h2 style="color: #2c3e50; border-bottom: 1px solid #ddd; padding-bottom: 8px;">Lifestyle Recommendations</h2><div style="background-color: #f2f6fc; padding: 15px; border-radius: 5px; margin-bottom: 25px;"><ul style="padding-left: 20px;">"""for item in lifestyle:html += f"<li>{item}</li>"html += """</ul></div>"""# Add urgent concernsurgent = analysis_result.get('urgent_concerns', [])if urgent and urgent != [None]:html += """<h2 style="color: #c0392b; border-bottom: 1px solid #ddd; padding-bottom: 8px;">β οΈ Urgent Considerations</h2><div style="background-color: #fdf2f0; padding: 15px; border-radius: 5px; border-left: 4px solid #c0392b; margin-bottom: 25px;"><ul style="padding-left: 20px;">"""for item in urgent:html += f"<li>{item}</li>"html += """</ul><p style="margin-top: 10px; font-weight: bold;">Please consult with a healthcare provider promptly regarding these concerns.</p></div>"""# Add follow-up recommendationsfollowup = analysis_result.get('followup_recommendations', {})if followup and followup.get('tests'):html += f"""<h2 style="color: #2c3e50; border-bottom: 1px solid #ddd; padding-bottom: 8px;">Follow-up Recommendations</h2><div style="background-color: #f9f9f9; padding: 15px; border-radius: 5px; margin-bottom: 25px;"><p><strong>Timeline:</strong> {followup.get('timeline', 'As advised by your healthcare provider')}</p><p><strong>Recommended Tests:</strong></p><ul style="padding-left: 20px;">"""for test in followup.get('tests', []):html += f"<li>{test}</li>"html += """</ul></div>"""# Disclaimerhtml += """<div style="border-top: 1px solid #ddd; margin-top: 30px; padding-top: 15px; font-size: 0.9em; color: #7f8c8d;"><p><strong>Disclaimer:</strong> This analysis is generated by an AI system and should not replace professional medical advice.Always consult with a healthcare provider before making any changes to your diet, lifestyle, or supplementation regimen.</p></div></div>"""return html# Calculate nutritional recommendations based on deficienciesdef calculate_recommendations(analysis_result, weight_kg=70, height_cm=165, activity_level="moderate"):if not analysis_result or "deficiencies" not in analysis_result:return None# Basic calculationsbmi = weight_kg / ((height_cm/100) ** 2)# Activity level multipliersactivity_multipliers = {"sedentary": 1.2,"light": 1.375,"moderate": 1.55,"active": 1.725,"very active": 1.9}# Calculate basal metabolic rate (BMR) using Mifflin-St Jeor equationbmr = 10 * weight_kg + 6.25 * height_cm - 5 * 30 + 5 # Assuming age 30 for example# Calculate total daily energy expendituretdee = bmr * activity_multipliers.get(activity_level.lower(), 1.55)# Create recommendation dictionaryrecommendations = {"anthropometrics": {"bmi": round(bmi, 1),"bmi_category": get_bmi_category(bmi),"estimated_energy_needs": round(tdee)},"supplements": []}# Process each deficiencyfor deficiency in analysis_result["deficiencies"]:nutrient = deficiency["nutrient"]severity = deficiency["severity"].lower()# Extract dosage value and unitdosage_match = re.search(r'(\d+[\.\d]*)\s*([a-zA-Z]+)', deficiency["recommended_dosage"])if dosage_match:amount = float(dosage_match.group(1))unit = dosage_match.group(2)# Adjust based on severityif severity == "severe":adjusted_amount = amount * 1.2 # 20% higher for severeelif severity == "mild":adjusted_amount = amount * 0.9 # 10% lower for mildelse:adjusted_amount = amountrecommendations["supplements"].append({"nutrient": nutrient,"dosage": f"{round(adjusted_amount, 2)} {unit}","original_dosage": f"{amount} {unit}","severity": severity,"duration": deficiency["supplementation_duration"],"frequency": "Daily","best_time": get_best_time_for_supplement(nutrient),"interactions": get_supplement_interactions(nutrient)})return recommendations# Helper functions for recommendationsdef get_bmi_category(bmi):if bmi < 18.5:return "Underweight"elif bmi < 25:return "Normal weight"elif bmi < 30:return "Overweight"else:return "Obese"def get_best_time_for_supplement(nutrient):# Time recommendations based on Indian contextnutrient_lower = nutrient.lower()if any(term in nutrient_lower for term in ["d", "a", "e", "k"]):return "With meals containing some fat (lunch or dinner)"elif "b12" in nutrient_lower:return "Morning, with breakfast"elif "iron" in nutrient_lower:return "On empty stomach, 1 hour before meals with Vitamin C"elif "calcium" in nutrient_lower:return "Between meals, avoid taking with iron supplements"elif "zinc" in nutrient_lower:return "1-2 hours after meals, not with calcium supplements"else:return "As directed by healthcare provider"def get_supplement_interactions(nutrient):# Common interactions for Indian medications and supplementsnutrient_lower = nutrient.lower()if "iron" in nutrient_lower:return ["Calcium supplements", "Tea/coffee", "Antacids", "Certain antibiotics"]elif "calcium" in nutrient_lower:return ["Iron supplements", "Certain antibiotics", "Thyroid medications"]elif "b12" in nutrient_lower:return ["Metformin", "Acid-reducing medications", "Colchicine"]elif "d" in nutrient_lower:return ["Steroids", "Weight loss medications", "Certain cholesterol medications"]else:return []# File upload handler for Hugging Facedef upload_and_process_file(file, api_key, name, age, gender):if not api_key:return "Please enter a valid Google API key", Nonetry:if file is None:return "No file was uploaded", None# Get file extensionfile_extension = file.name.split('.')[-1].lower()file_content = file.read()# Process based on file typeif file_extension == 'pdf':report_text, extracted_images, tables = extract_text_from_pdf(file_content)extracted_markers = extract_blood_markers(report_text)vision_model, text_model = configure_genai(api_key)# If text extraction worked well and we found markersif len(extracted_markers) > 0:analysis_result = analyze_report(vision_model, text_model, report_text, extracted_markers, is_text=True)# If text extraction didn't yield much, use the imageselif extracted_images:# Use the first image as primary, but include text contextanalysis_result = analyze_report(vision_model, text_model,[report_text, extracted_images[0]],extracted_markers)else:return "Could not extract sufficient data from the PDF. Please try uploading a clearer document.", Noneelif file_extension in ['jpg', 'jpeg', 'png']:img = Image.open(io.BytesIO(file_content))processed_img = preprocess_image(img)vision_model, text_model = configure_genai(api_key)analysis_result = analyze_report(vision_model, text_model, processed_img, {})else:return f"Unsupported file format: {file_extension}. Please upload a PDF or image (JPG, PNG).", None# Create patient info dictionary if providedpatient_info = Noneif name or age or gender:patient_info = {"name": name,"age": age,"gender": gender}# Generate HTML reporthtml_report = generate_recommendation_html(analysis_result, patient_info)return html_report, analysis_resultexcept Exception as e:return f"An error occurred: {str(e)}", None# Create the Gradio Interface for Hugging Facedef create_interface():with gr.Blocks(theme=gr.themes.Soft(primary_hue="indigo")) as app:gr.Markdown("""# π©Έ Blood Report Analyzer## Analyze blood test reports for vitamin deficiencies and get personalized recommendationsThis application uses Gemini AI to analyze your blood test results and provide detailed insightson nutritional deficiencies with recommendations tailored to Indian health needs.""")with gr.Tab("π Report Analysis"):with gr.Row():with gr.Column(scale=1):api_key = gr.Textbox(label="Google Gemini API Key",placeholder="Enter your Gemini API key",type="password")with gr.Accordion("Instructions for Using This Tool", open=False):gr.Markdown("""## How to Use This Tool### 1. Prepare Your Report- Ensure your blood report is clear and readable- PDF format is preferred- If using images, ensure good lighting and focus### 2. Get a Gemini API Key- Visit [Google AI Studio](https://ai.google.dev/)- Create an account or sign in- Navigate to API keys and create a new key### 3. Upload and Analyze- Enter your API key in the designated field- (Optional) Enter patient information for personalized results- Upload your blood report file- Click "Analyze Report"### 4. Review Results- The analysis will display deficiencies found, their severity, and recommendations- For personalized supplementation, enter weight, height, and activity level- Click "Generate Supplement Plan" for customized dosage recommendations### 5. Share Results- You can save the HTML report by right-clicking and selecting "Save as"- Share the results with your healthcare provider### Important Notes- This tool is for informational purposes only- Always consult with healthcare professionals before making health decisions- Your data is not stored and is only used for analysis""")with gr.Row():with gr.Column(scale=1):with gr.Group():gr.Markdown("### Patient Information (Optional)")name = gr.Textbox(label="Name", placeholder="Enter patient name")with gr.Row():age = gr.Textbox(label="Age", placeholder="e.g., 35")gender = gr.Dropdown(label="Gender", choices=["Male", "Female", "Other"], value="Male")upload_file = gr.File(label="Upload Blood Report")analyze_button = gr.Button("π Analyze Report", variant="primary")with gr.Column(scale=2):output = gr.HTML(label="Analysis Results")raw_output = gr.JSON(label="Raw Analysis Data", visible=False)with gr.Row():with gr.Column():with gr.Group():gr.Markdown("### Supplement Recommendations")with gr.Row():weight = gr.Number(label="Weight (kg)", value=70)height = gr.Number(label="Height (cm)", value=165)activity = gr.Dropdown(label="Activity Level",choices=["Sedentary", "Light", "Moderate", "Active", "Very Active"],value="Moderate")supplement_button = gr.Button("π Generate Supplement Plan")supplement_output = gr.JSON(label="Personalized Supplement Plan")# Connect the buttons to functionsanalyze_button.click(fn=upload_and_process_file,inputs=[upload_file, api_key, name, age, gender],outputs=[output, raw_output])supplement_button.click(fn=calculate_recommendations,inputs=[raw_output, weight, height, activity],outputs=[supplement_output])with gr.Tab("π About"):gr.Markdown("""## About Blood Report AnalyzerThis tool was developed to help people in India better understand their blood test results,with a focus on identifying nutritional deficiencies that are common in the Indian population.### How it Works1. The tool uses advanced OCR and AI to extract relevant information from your blood report2. Google's Gemini AI models analyze the data to identify deficiencies3. Recommendations are tailored to the Indian context, including:- Locally available foods- Cultural dietary considerations- Regional supplementation guidelines### Privacy & Security- Your data remains private and is not stored- Analysis happens in real-time- API keys are only used for processing and are not saved### Limitations- This tool is for informational purposes only- It does not replace medical advice from healthcare professionals- Accuracy depends on the quality of the uploaded report- Some rare deficiencies may not be detected### AcknowledgementsThis application uses Google's Gemini AI models and is built with Gradio for Hugging Face Spaces.""")return app# Export the interfaceapp = create_interface()# Launch the appif __name__ == "__main__":app.launch()app.launch(share=True)
app.py+740-60