Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| import torch | |
| import json | |
| import os | |
| import re | |
| from huggingface_hub import login | |
| #hf_token = os.getenv("HK_TOKEN_MISTRL") # Fetch Hugging Face API token | |
| login(token=os.getenv("HK_TOKEN_MISTRL")) | |
| # Load the model and tokenizer | |
| model_name = "mistralai/Mistral-7B-Instruct-v0.2" | |
| tokenizer = AutoTokenizer.from_pretrained(model_name) | |
| model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto") | |
| def compare_claims(claim_json1, claim_json2): | |
| """ | |
| Compare two health insurance claims using OpenAI GPT model and return structured JSON output. | |
| :param claim_json1: JSON string for first claim | |
| :param claim_json2: JSON string for second claim | |
| :return: JSON output with similarities, differences, and summary | |
| """ | |
| # Parse input JSONs | |
| claim1 = json.loads(claim_json1) | |
| claim2 = json.loads(claim_json2) | |
| # Structured prompt for better JSON output | |
| prompt = f""" | |
| Compare the following insurance claims and identify similarities and differences. | |
| Claim 1: | |
| {json.dumps(claim1, indent=2)} | |
| Claim 2: | |
| {json.dumps(claim2, indent=2)} | |
| Provide a structured comparison highlighting commonalities and differences. | |
| """ | |
| device = "cuda" if torch.cuda.is_available() else "cpu" # Automatically select CPU if no GPU | |
| # Tokenize input correctly | |
| inputs = tokenizer(prompt, return_tensors="pt") # Convert prompt to tensor | |
| input_ids = inputs["input_ids"].to(device) # Move to CPU instead of GPU | |
| inputs["attention_mask"] = inputs["attention_mask"].to(device) | |
| # Generate response | |
| output = model.generate(input_ids=input_ids, max_new_tokens=300, pad_token_id=tokenizer.eos_token_id) | |
| # Decode output | |
| response = tokenizer.decode(output[0], skip_special_tokens=True) | |
| return response # This should now work correctly! | |
| def main(): | |
| """ | |
| Launch the Gradio interface for claim comparison. | |
| """ | |
| # Define the Gradio interface | |
| interface = gr.Interface( | |
| fn=compare_claims, | |
| inputs=[ | |
| gr.Textbox(label="claim_json1", placeholder="Enter first claim description..."), | |
| gr.Textbox(label="claim_json2", placeholder="Enter second claim description...") | |
| ], | |
| outputs="text", | |
| title="Claims Comparison", | |
| description="Enter two claims to compare their differences." | |
| ) | |
| # Launch the Gradio app | |
| interface.launch() | |
| if __name__ == "__main__": | |
| main() | |