gauravchand11 commited on
Commit
7b3a738
·
verified ·
1 Parent(s): a60593a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +132 -8
app.py CHANGED
@@ -1,8 +1,132 @@
1
- import gradio as gr
2
- from ui import create_interface
3
-
4
- # Create the interface
5
- demo = create_interface()
6
-
7
- if __name__ == "__main__":
8
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
+ import torch
4
+ import pytesseract
5
+ from PIL import Image
6
+ import PyPDF2
7
+ import io
8
+ import requests
9
+ import os
10
+
11
+ # Azure Translator API Configuration
12
+ AZURE_TRANSLATOR_KEY = "your_azure_key"
13
+ AZURE_TRANSLATOR_ENDPOINT = "https://api.cognitive.microsofttranslator.com/translate"
14
+ AZURE_TRANSLATOR_REGION = "your_region"
15
+
16
+ # Specify the model
17
+ MODEL_NAME = "google/gemma-2b-it"
18
+
19
+ class LegalEaseAssistant:
20
+ def __init__(self, model_name=MODEL_NAME):
21
+ self.tokenizer = AutoTokenizer.from_pretrained(model_name)
22
+ self.model = AutoModelForCausalLM.from_pretrained(
23
+ model_name,
24
+ device_map="cpu",
25
+ load_in_8bit=True,
26
+ torch_dtype=torch.float16
27
+ )
28
+
29
+ def extract_text_from_input(self, input_file):
30
+ if isinstance(input_file, str):
31
+ return input_file
32
+
33
+ if isinstance(input_file, Image.Image):
34
+ try:
35
+ return pytesseract.image_to_string(input_file)
36
+ except Exception as e:
37
+ return f"Error extracting text from image: {str(e)}"
38
+
39
+ if hasattr(input_file, 'name') and input_file.name.lower().endswith('.pdf'):
40
+ try:
41
+ pdf_reader = PyPDF2.PdfReader(input_file)
42
+ text = ""
43
+ for page in pdf_reader.pages:
44
+ text += page.extract_text() + "\n\n"
45
+ return text
46
+ except Exception as e:
47
+ return f"Error extracting text from PDF: {str(e)}"
48
+
49
+ return "Unsupported input type"
50
+
51
+ def generate_response(self, input_file, task_type):
52
+ text = self.extract_text_from_input(input_file)
53
+
54
+ task_prompts = {
55
+ "simplify": f"Simplify the following legal text:\n\n{text}\n\nSimplified explanation:",
56
+ "summary": f"Provide a concise summary:\n\n{text}\n\nSummary:",
57
+ "key_terms": f"Identify key legal terms:\n\n{text}\n\nKey Terms:",
58
+ "risk": f"Perform a risk analysis:\n\n{text}\n\nRisk Assessment:"
59
+ }
60
+
61
+ prompt = task_prompts.get(task_type, f"Analyze the following text:\n\n{text}\n\nAnalysis:")
62
+
63
+ inputs = self.tokenizer(prompt, return_tensors="pt")
64
+ outputs = self.model.generate(
65
+ **inputs,
66
+ max_new_tokens=300,
67
+ num_return_sequences=1,
68
+ do_sample=True,
69
+ temperature=0.7,
70
+ top_p=0.9
71
+ )
72
+
73
+ response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
74
+ response_parts = response.split(prompt.split("\n\n")[-1])
75
+ return response_parts[-1].strip() if len(response_parts) > 1 else response.strip()
76
+
77
+ def translate_text(self, text, target_language):
78
+ if not text:
79
+ return "No text provided for translation."
80
+
81
+ params = {'api-version': '3.0', 'to': target_language}
82
+ headers = {
83
+ 'Ocp-Apim-Subscription-Key': AZURE_TRANSLATOR_KEY,
84
+ 'Ocp-Apim-Subscription-Region': AZURE_TRANSLATOR_REGION,
85
+ 'Content-Type': 'application/json'
86
+ }
87
+ body = [{'text': text}]
88
+
89
+ try:
90
+ response = requests.post(AZURE_TRANSLATOR_ENDPOINT, params=params, headers=headers, json=body)
91
+ response_data = response.json()
92
+ return response_data[0]['translations'][0]['text']
93
+ except Exception as e:
94
+ return f"Error translating text: {str(e)}"
95
+
96
+ # Create Gradio Interface
97
+ def create_interface():
98
+ assistant = LegalEaseAssistant()
99
+
100
+ with gr.Blocks(title="LegalEase: AI Legal Assistant") as demo:
101
+ gr.Markdown("# 📜 LegalEase: AI-Powered Legal Document Assistant")
102
+
103
+ with gr.Tabs():
104
+ for task_name, task_type in [("Simplify Language", "simplify"), ("Document Summary", "summary"),
105
+ ("Key Terms", "key_terms"), ("Risk Analysis", "risk")]:
106
+ with gr.Tab(task_name):
107
+ with gr.Row():
108
+ input_file = gr.File(file_types=['txt', 'pdf', 'image'], label="Upload Document")
109
+ input_text = gr.Textbox(label="Or Paste Text", lines=3)
110
+ output_text = gr.Textbox(label="Generated Output", lines=6)
111
+
112
+ language_dropdown = gr.Dropdown(choices=["en", "hi", "mr"], value="en", label="Translate To")
113
+ translated_output = gr.Textbox(label="Translated Output", lines=6)
114
+ btn = gr.Button(f"Generate {task_name}")
115
+
116
+ def handler(file, text, language):
117
+ input_source = file or text
118
+ if not input_source:
119
+ return "", ""
120
+ generated_text = assistant.generate_response(input_source, task_type)
121
+ translated_text = assistant.translate_text(generated_text, language) if language != "en" else generated_text
122
+ return generated_text, translated_text
123
+
124
+ btn.click(fn=handler, inputs=[input_file, input_text, language_dropdown], outputs=[output_text, translated_output])
125
+
126
+ return demo
127
+
128
+ # Create and launch the app
129
+ demo = create_interface()
130
+
131
+ if __name__ == "__main__":
132
+ demo.launch()