Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,55 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
import torch
|
4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
5 |
|
6 |
+
#Setting device to cuda
|
7 |
+
torch.set_default_device("cuda")
|
8 |
+
|
9 |
+
# # Ensure GPU usage if available
|
10 |
+
# device = "cuda" if torch.cuda.is_available() else "cpu"
|
11 |
+
# torch.set_default_tensor_type('torch.cuda.FloatTensor' if device=='cuda' else 'torch.FloatTensor')
|
12 |
+
|
13 |
+
torch.set_default_tensor_type('torch.cuda.FloatTensor')
|
14 |
+
|
15 |
+
|
16 |
+
|
17 |
+
# Initialize the model and tokenizer
|
18 |
+
model = AutoModelForCausalLM.from_pretrained("ManishThota/Thota",
|
19 |
+
torch_dtype=torch.float16,
|
20 |
+
device_map="auto",
|
21 |
+
trust_remote_code=True)
|
22 |
+
tokenizer = AutoTokenizer.from_pretrained("ManishThota/Thota", trust_remote_code=True)
|
23 |
+
|
24 |
+
def predict_answer(question, max_tokens):
|
25 |
+
#Set inputs
|
26 |
+
text = f"A chat between a curious user and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's questions. USER: <image>\n{question}? ASSISTANT:"
|
27 |
+
image = image.convert("RGB")
|
28 |
+
|
29 |
+
input_ids = tokenizer(text, return_tensors='pt').input_ids.to('cuda')
|
30 |
+
|
31 |
+
#Generate the answer
|
32 |
+
output_ids = model.generate(
|
33 |
+
input_ids,
|
34 |
+
max_new_tokens=max_tokens,
|
35 |
+
images=image_tensor,
|
36 |
+
use_cache=True)[0]
|
37 |
+
|
38 |
+
return tokenizer.decode(output_ids[input_ids.shape[1]:], skip_special_tokens=True).strip()
|
39 |
+
|
40 |
+
def gradio_predict(question, max_tokens):
|
41 |
+
answer = predict_answer(question, max_tokens)
|
42 |
+
return answer
|
43 |
+
|
44 |
+
# Define the Gradio interface
|
45 |
+
iface = gr.Interface(
|
46 |
+
fn=gradio_predict,
|
47 |
+
inputs=[ gr.Textbox(label="Question", placeholder="e.g. Who is Manish Kumar Thota?", scale=4),
|
48 |
+
gr.Slider(2, 100, value=25, label="Token Count", info="Choose between 2 and 100")],
|
49 |
+
outputs=gr.TextArea(label="Answer"),
|
50 |
+
title="Welcome to Manish Kumar Thota's Portfolio",
|
51 |
+
description="Ask questions about Manish and get to know him as if you are directly talking to him, this model is still in development so your feedback is highly valuable to improve the model.",
|
52 |
+
)
|
53 |
+
|
54 |
+
# Launch the app
|
55 |
+
iface.queue().launch(debug=True)
|