jhansi1 commited on
Commit
219c6cc
1 Parent(s): 5110eec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -61
app.py CHANGED
@@ -1,63 +1,10 @@
1
- import gradio as gr
2
- from transformers import AutoTokenizer, AutoModelForPreTraining, pipeline
3
- from huggingface_hub import InferenceClient
4
- from typing import List, Tuple
5
 
6
- # Load the InLegalBERT model and tokenizer
7
- tokenizer = AutoTokenizer.from_pretrained("law-ai/InLegalBERT")
8
- model = AutoModelForPreTraining.from_pretrained("law-ai/InLegalBERT")
9
 
10
- # Initialize the Gradio interface
11
- def respond(
12
- message: str,
13
- history: List[Tuple[str, str]], # Using List and Tuple for type annotation
14
- system_message: str,
15
- max_tokens: int,
16
- temperature: float,
17
- top_p: float,
18
- ):
19
- # Start with system message
20
- messages = [{"role": "system", "content": system_message}]
21
-
22
- # Append history to the messages list
23
- for val in history:
24
- if val[0]:
25
- messages.append({"role": "user", "content": val[0]})
26
- if val[1]:
27
- messages.append({"role": "assistant", "content": val[1]})
28
-
29
- # Append the current user message
30
- messages.append({"role": "user", "content": message})
31
-
32
- response = ""
33
-
34
- # Use the tokenizer to process the input message
35
- inputs = tokenizer(message, return_tensors="pt")
36
-
37
- # Use the model to generate a response
38
- with torch.no_grad(): # Disable gradients since we're just doing inference
39
- outputs = model(**inputs)
40
- response = tokenizer.decode(outputs.logits.argmax(dim=-1)[0], skip_special_tokens=True)
41
-
42
- yield response
43
-
44
- # Set up Gradio interface with additional options
45
- demo = gr.ChatInterface(
46
- respond,
47
- additional_inputs=[
48
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
49
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
50
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
51
- gr.Slider(
52
- minimum=0.1,
53
- maximum=1.0,
54
- value=0.95,
55
- step=0.05,
56
- label="Top-p (nucleus sampling)",
57
- ),
58
- ],
59
- )
60
-
61
- # Launch the Gradio app
62
- if __name__ == "__main__":
63
- demo.launch()
 
1
+ from huggingface_hub import hf_hub_download
2
+ from transformers import AutoModelForPreTraining, AutoTokenizer
 
 
3
 
4
+ # Download model and tokenizer
5
+ model_path = hf_hub_download("law-ai/InLegalBERT", filename="pytorch_model.bin")
6
+ tokenizer_path = hf_hub_download("law-ai/InLegalBERT", filename="tokenizer_config.json")
7
 
8
+ # Load locally downloaded model and tokenizer
9
+ tokenizer = AutoTokenizer.from_pretrained(tokenizer_path)
10
+ model = AutoModelForPreTraining.from_pretrained(model_path)