Praveen0309 commited on
Commit
165a317
·
1 Parent(s): 2935628

Add application1 file

Browse files
Files changed (2) hide show
  1. app.py +92 -4
  2. requirements.txt +0 -0
app.py CHANGED
@@ -1,7 +1,95 @@
 
 
 
 
 
 
 
1
  import gradio as gr
 
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import AutoTokenizer, AutoProcessor, TrainingArguments, LlavaForConditionalGeneration, BitsAndBytesConfig
3
+ from trl import SFTTrainer
4
+ from peft import LoraConfig, PeftModel
5
+ from PIL import Image
6
+ import requests
7
+ from deep_translator import GoogleTranslator
8
  import gradio as gr
9
+ import PIL.Image
10
+ import base64
11
+ import time
12
+ import os
13
 
 
 
14
 
15
+ model_id = "HuggingFaceH4/vsft-llava-1.5-7b-hf-trl"
16
+ quantization_config = BitsAndBytesConfig(load_in_4bit=True)
17
+ base_model = LlavaForConditionalGeneration.from_pretrained(model_id, quantization_config=quantization_config, torch_dtype=torch.float16)
18
+
19
+ # Load the PEFT Lora adapter
20
+ peft_lora_adapter_path = "Praveen0309/llava-1.5-7b-hf-ft-mix-vsft-3"
21
+ peft_lora_adapter = PeftModel.from_pretrained(base_model, peft_lora_adapter_path, adapter_name="lora_adapter")
22
+ base_model.load_adapter(peft_lora_adapter_path, adapter_name="lora_adapter")
23
+
24
+ processor = AutoProcessor.from_pretrained("HuggingFaceH4/vsft-llava-1.5-7b-hf-trl")
25
+
26
+ # Function to translate text from Bengali to English
27
+ def deep_translator_bn_en(input_sentence):
28
+ english_translation = GoogleTranslator(source="bn", target="en").translate(input_sentence)
29
+ return english_translation
30
+
31
+ # Function to translate text from English to Bengali
32
+ def deep_translator_en_bn(input_sentence):
33
+ bengali_translation = GoogleTranslator(source="en", target="bn").translate(input_sentence)
34
+ return bengali_translation
35
+
36
+ def inference(image, image_prompt):
37
+ prompt = f"USER: <image>\n{image_prompt} ASSISTANT:"
38
+
39
+ # Assuming your model can handle PIL images
40
+ image = image.convert("RGB") # Ensure image is RGB mode
41
+
42
+ inputs = processor(text=prompt, images=image, return_tensors="pt")
43
+ generate_ids = base_model.generate(**inputs, max_new_tokens=15)
44
+ decoded_response = processor.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
45
+ return decoded_response
46
+
47
+ def image_to_base64(image_path):
48
+ with open(image_path, 'rb') as img:
49
+ encoded_string = base64.b64encode(img.read())
50
+ return encoded_string.decode('utf-8')
51
+
52
+ # Function that takes User Inputs and displays it on ChatUI
53
+ def query_message(history,txt,img):
54
+ image_prompt = deep_translator_bn_en(txt)
55
+ history += [(image_prompt,None)]
56
+ base64 = image_to_base64(img)
57
+ data_url = f"data:image/jpeg;base64,{base64}"
58
+ history += [(f"{image_prompt} ![]({data_url})", None)]
59
+ return history
60
+
61
+ # Function that takes User Inputs, generates Response and displays on Chat UI
62
+ def llm_response(history,text,img):
63
+ image_prompt = deep_translator_bn_en(text)
64
+ response = inference(img,image_prompt)
65
+ assistant_index = response.find("ASSISTANT:")
66
+ extracted_string = response[assistant_index + len("ASSISTANT:"):].strip()
67
+ output = deep_translator_en_bn(extracted_string)
68
+ history += [(text,output)]
69
+ return history
70
+
71
+ # Interface Code
72
+ with gr.Blocks() as app:
73
+ with gr.Row():
74
+ image_box = gr.Image(type="pil")
75
+
76
+ chatbot = gr.Chatbot(
77
+ scale = 2,
78
+ height=500
79
+ )
80
+ text_box = gr.Textbox(
81
+ placeholder="Enter text and press enter, or upload an image",
82
+ container=False,
83
+ )
84
+
85
+ btn = gr.Button("Submit")
86
+ clicked = btn.click(query_message,
87
+ [chatbot,text_box,image_box],
88
+ chatbot
89
+ ).then(llm_response,
90
+ [chatbot,text_box,image_box],
91
+ chatbot
92
+ )
93
+ app.queue()
94
+ app.launch(debug=True,share=True)
95
+
requirements.txt ADDED
Binary file (3.74 kB). View file