Tonic commited on
Commit
bc93368
1 Parent(s): 454c3f9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +110 -0
app.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoConfig, AutoTokenizer, AutoModelForSeq2SeqLM, AutoModelForCausalLM, MistralForCausalLM
2
+ import torch
3
+ import gradio as gr
4
+ import random
5
+ from textwrap import wrap
6
+ import spaces
7
+
8
+ def wrap_text(text, width=90):
9
+ lines = text.split('\n')
10
+ wrapped_lines = [textwrap.fill(line, width=width) for line in lines]
11
+ wrapped_text = '\n'.join(wrapped_lines)
12
+ return wrapped_text
13
+
14
+ @spaces.GPU
15
+ def multimodal_prompt(user_input, system_prompt="You are an expert medical analyst:"):
16
+ # Combine user input and system prompt
17
+ formatted_input = f"<s> [INST] {example_instruction} [/INST] {example_answer}</s> [INST] {system_prompt} [/INST]"
18
+
19
+ # Encode the input text
20
+ encodeds = tokenizer(formatted_input, return_tensors="pt", add_special_tokens=False)
21
+ model_inputs = encodeds.to(device)
22
+
23
+ # Generate a response using the model
24
+ output = model.generate(
25
+ **model_inputs,
26
+ max_length=max_length,
27
+ use_cache=True,
28
+ early_stopping=True,
29
+ bos_token_id=model.config.bos_token_id,
30
+ eos_token_id=model.config.eos_token_id,
31
+ pad_token_id=model.config.eos_token_id,
32
+ temperature=0.1,
33
+ do_sample=True
34
+ )
35
+
36
+ # Decode the response
37
+ response_text = tokenizer.decode(output[0], skip_special_tokens=True)
38
+
39
+ return response_text
40
+
41
+ # Define the device
42
+ device = "cuda" if torch.cuda.is_available() else "cpu"
43
+
44
+ # Use the base model's ID
45
+ model_id = "SuperAGI/SAM"
46
+
47
+ tokenizer = AutoTokenizer.from_pretrained(model_id = model_id, trust_remote_code=True)
48
+ # tokenizer.pad_token = tokenizer.eos_token
49
+ # tokenizer.padding_side = 'left'
50
+
51
+ # Specify the configuration class for the model
52
+ #model_config = AutoConfig.from_pretrained(base_model_id)
53
+
54
+ model = MistralForCaumodel = AutoModelForCausalLM.from_pretrained(model_id)
55
+
56
+ class ChatBot:
57
+ def __init__(self):
58
+ self.history = []
59
+
60
+ class ChatBot:
61
+ def __init__(self):
62
+ # Initialize the ChatBot class with an empty history
63
+ self.history = []
64
+
65
+ def predict(self, user_input, system_prompt="You are an expert medical analyst:"):
66
+ # Combine the user's input with the system prompt
67
+ formatted_input = f"<s>[INST]{system_prompt} {user_input}[/INST]"
68
+
69
+ # Encode the formatted input using the tokenizer
70
+ user_input_ids = tokenizer.encode(formatted_input, return_tensors="pt")
71
+
72
+ # Generate a response using the PEFT model
73
+ response = peft_model.generate(input_ids=user_input_ids, max_length=512, pad_token_id=tokenizer.eos_token_id)
74
+
75
+ # Decode the generated response to text
76
+ response_text = tokenizer.decode(response[0], skip_special_tokens=True)
77
+
78
+ return response_text # Return the generated response
79
+
80
+ bot = ChatBot()
81
+
82
+ title = "🚀👋🏻Welcome to Tonic's🤖SuperAGI/SAM Chat🚀"
83
+ description = "SAM is an Agentic-Native LLM that excels at complex reasoning. You can use this Space to test out the current model [Tonic/superagi-sam](https://huggingface.co/Tonic/superagi-sam) or duplicate this Space and use it locally or on 🤗HuggingFace. [Join me on Discord to build together](https://discord.gg/VqTxc76K3u)."
84
+ examples = [["[Question:] What is the proper treatment for buccal herpes?", "You are a medicine and public health expert, you will receive a question, answer the question, and provide a complete answer"]]
85
+
86
+
87
+ def main():
88
+ with gr.Blocks() as demo:
89
+ gr.Markdown(title)
90
+ gr.Markdown(description)
91
+ with gr.Row():
92
+ with gr.Column():
93
+ example_instruction = gr.Textbox(label="Example Instruction")
94
+ example_answer = gr.Textbox(label="Example Answer")
95
+ with gr.Column():
96
+ user_input = gr.Textbox(label="Your Question")
97
+ system_prompt = gr.Textbox(label="System Prompt", value="You are an expert medical analyst:")
98
+ submit_btn = gr.Button("Submit")
99
+ output = gr.Textbox(label="Response")
100
+
101
+ submit_btn.click(
102
+ fn=bot.predict,
103
+ inputs=[example_instruction, example_answer, user_input, system_prompt],
104
+ outputs=output
105
+ )
106
+
107
+ demo.launch()
108
+
109
+ if __name__ == "__main__":
110
+ main()