Csplk commited on
Commit
201e079
β€’
1 Parent(s): e9ecb71

Delete originalapp.py

Browse files
Files changed (1) hide show
  1. originalapp.py +0 -59
originalapp.py DELETED
@@ -1,59 +0,0 @@
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
- model_id = "vikhyatk/moondream2"
12
- revision = "2024-04-02"
13
- tokenizer = AutoTokenizer.from_pretrained(model_id, revision=revision)
14
- moondream = AutoModelForCausalLM.from_pretrained(
15
- model_id, trust_remote_code=True, revision=revision,
16
- torch_dtype=torch.float32
17
- )
18
- moondream.eval()
19
-
20
-
21
- @spaces.GPU(duration=10)
22
- def answer_question(img, prompt):
23
- image_embeds = moondream.encode_image(img)
24
- streamer = TextIteratorStreamer(tokenizer, skip_special_tokens=True)
25
- thread = Thread(
26
- target=moondream.answer_question,
27
- kwargs={
28
- "image_embeds": image_embeds,
29
- "question": prompt,
30
- "tokenizer": tokenizer,
31
- "streamer": streamer,
32
- },
33
- )
34
- thread.start()
35
-
36
- buffer = ""
37
- for new_text in streamer:
38
- clean_text = re.sub("<$|<END$", "", new_text)
39
- buffer += clean_text
40
- yield buffer
41
-
42
-
43
- with gr.Blocks() as demo:
44
- gr.Markdown(
45
- """
46
- # πŸŒ” moondream2
47
- A tiny vision language model. [GitHub](https://github.com/vikhyat/moondream)
48
- """
49
- )
50
- with gr.Row():
51
- prompt = gr.Textbox(label="Input", placeholder="Type here...", scale=4)
52
- submit = gr.Button("Submit")
53
- with gr.Row():
54
- img = gr.Image(type="pil", label="Upload an Image")
55
- output = gr.TextArea(label="Response")
56
- submit.click(answer_question, [img, prompt], output)
57
- prompt.submit(answer_question, [img, prompt], output)
58
-
59
- demo.queue().launch()