alibidaran commited on
Commit
ad8cb2c
1 Parent(s): 31bb39a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -0
app.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from ctransformers import AutoModelForCausalLM
2
+ from transformers import AutoTokenizer
3
+ import torch
4
+ import gradio as gr
5
+ import os
6
+ import time
7
+ model = AutoModelForCausalLM.from_pretrained("alibidaran/llama-2-7b-virtual_doctor-gguf",hf=True)
8
+ tokenizer = AutoTokenizer.from_pretrained("alibidaran/llama-2-7b-virtual_doctor")
9
+ def print_like_dislike(x: gr.LikeData):
10
+ print(x.index, x.value, x.liked)
11
+
12
+
13
+ def add_text(history, text):
14
+ history = history + [(text,None)]
15
+ return history, gr.Textbox(value="", interactive=False)
16
+
17
+
18
+ def add_file(history, file):
19
+ global image_file
20
+ image_file=file.name
21
+ history = history + [((file.name,),None)]
22
+ return history
23
+
24
+
25
+ def bot(history):
26
+ prompt=history[-1][0]
27
+ text=f"<s> ###Human: {prompt} ###Asistant: "
28
+ inputs=tokenizer(text,return_tensors='pt').to('cpu')
29
+ with torch.no_grad():
30
+ outputs=model.generate(**inputs,max_new_tokens=200,do_sample=True,top_p=0.92,top_k=10,temperature=0.7)
31
+ response=tokenizer.decode(outputs[0], skip_special_tokens=True)
32
+ print(response)
33
+ history[-1][1] = ""
34
+ for character in response[1:-1]:
35
+ history[-1][1] += character
36
+ time.sleep(0.01)
37
+ yield history
38
+
39
+
40
+ with gr.Blocks() as demo:
41
+ chatbot = gr.Chatbot(
42
+ [],
43
+ elem_id="chatbot",
44
+ bubble_full_width=False,
45
+ #avatar_images=(None, (os.path.join(os.path.dirname(__file__), "avatar.png"))),
46
+ )
47
+
48
+ with gr.Row():
49
+ txt = gr.Textbox(
50
+ scale=4,
51
+ show_label=False,
52
+ placeholder="Enter text and press enter, or upload an image",
53
+ container=False,
54
+ )
55
+ btn = gr.UploadButton("📁", file_types=["image", "video", "audio"])
56
+
57
+ txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
58
+ bot, chatbot, chatbot, api_name="bot_response"
59
+ )
60
+ txt_msg.then(lambda: gr.Textbox(interactive=True), None, [txt], queue=False)
61
+ file_msg = btn.upload(add_file, [chatbot, btn], [chatbot], queue=False).then(
62
+ bot, chatbot, chatbot
63
+ )
64
+
65
+ chatbot.like(print_like_dislike, None, None)
66
+
67
+
68
+
69
+ if __name__=="__main__":
70
+ demo.launch(share=True,debug=True)