kadirnar commited on
Commit
60e4461
1 Parent(s): 9053590

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import spaces
2
+ import argparse
3
+ import torch
4
+ import re
5
+ import gradio as gr
6
+ from threading import Thread
7
+ from transformers import TextIteratorStreamer, AutoTokenizer, AutoModelForCausalLM
8
+
9
+ parser = argparse.ArgumentParser()
10
+
11
+ if torch.cuda.is_available():
12
+ device, dtype = "cuda", torch.float16
13
+ else:
14
+ device, dtype = "cpu", torch.float32
15
+
16
+ model_id = "gokaygokay/moondream-prompt"
17
+ revision = "1082928d0aa39290a31a92f632ca670458eda512"
18
+ tokenizer = AutoTokenizer.from_pretrained(model_id, revision=revision)
19
+ moondream = AutoModelForCausalLM.from_pretrained(model_id,trust_remote_code=True, revision=revision).to(device=device, dtype=dtype)
20
+ moondream.eval()
21
+
22
+
23
+ @spaces.GPU(duration=10)
24
+ def answer_question(img, prompt):
25
+ image_embeds = moondream.encode_image(img)
26
+ streamer = TextIteratorStreamer(tokenizer, skip_special_tokens=True)
27
+ thread = Thread(
28
+ target=moondream.answer_question,
29
+ kwargs={
30
+ "image_embeds": image_embeds,
31
+ "question": prompt,
32
+ "tokenizer": tokenizer,
33
+ "streamer": streamer,
34
+ },
35
+ )
36
+ thread.start()
37
+
38
+ buffer = ""
39
+ for new_text in streamer:
40
+ clean_text = re.sub("<$|<END$", "", new_text)
41
+ buffer += clean_text
42
+ yield buffer
43
+
44
+
45
+ with gr.Blocks() as demo:
46
+ gr.Markdown(
47
+ """
48
+ Fine tuned version of # 🌔 moondream2 for prompt generation.
49
+ Moondream is a tiny vision language model. [GitHub](https://github.com/vikhyat/moondream)
50
+ """
51
+ )
52
+ with gr.Row():
53
+ prompt = gr.Textbox(label="Input", placeholder="Type here...", scale=4)
54
+ submit = gr.Button("Submit")
55
+ with gr.Row():
56
+ img = gr.Image(type="pil", label="Upload an Image")
57
+ output = gr.TextArea(label="Response")
58
+ submit.click(answer_question, [img, prompt], output)
59
+ prompt.submit(answer_question, [img, prompt], output)
60
+
61
+ demo.queue().launch()