Tonic commited on
Commit
6e4750f
1 Parent(s): fd5c68e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -65
app.py CHANGED
@@ -1,88 +1,44 @@
1
  import spaces
2
- from transformers import AutoConfig, AutoTokenizer, AutoModelForSeq2SeqLM, AutoModelForCausalLM, MistralForCausalLM
3
  import torch
4
  import gradio as gr
5
- import random
6
- from textwrap import wrap
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)
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 = AutoModelForCausalLM.from_pretrained(model_id , torch_dtype=torch.float16 , device_map= "auto" )
 
55
 
 
56
 
57
  class ChatBot:
58
  def __init__(self):
59
- # Initialize the ChatBot class with an empty history
60
  self.history = []
61
 
62
- def predict(self, user_input, system_prompt="You are an expert medical analyst:" , example_instruction="produce a json", example_answer = "please dont make small talk "):
63
- # Combine the user's input with the system prompt
64
- formatted_input = f"<s> [INST] {example_instruction} [/INST] {example_answer}</s> [INST] {system_prompt} [/INST]"
65
-
66
- # Encode the formatted input using the tokenizer
67
- user_input_ids = tokenizer.encode(formatted_input, return_tensors="pt")
68
-
69
- # Generate a response using the PEFT model
70
- response = model.generate(input_ids=user_input_ids, max_length=512, pad_token_id=tokenizer.eos_token_id)
71
-
72
- # Decode the generated response to text
73
- response_text = tokenizer.decode(response[0], skip_special_tokens=True)
74
-
75
- return response_text # Return the generated response
76
 
77
  bot = ChatBot()
78
 
79
- title = "🚀👋🏻Welcome to Tonic's🤖SuperAGI/SAM Chat🚀"
80
- 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)."
81
- 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"]]
82
-
83
-
84
  def main():
85
  with gr.Blocks() as demo:
 
86
  gr.Markdown(title)
87
  gr.Markdown(description)
88
  with gr.Row():
@@ -104,4 +60,4 @@ def main():
104
  demo.launch()
105
 
106
  if __name__ == "__main__":
107
- main()
 
1
  import spaces
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
  import torch
4
  import gradio as gr
 
 
5
 
6
+ title = "# 🚀👋🏻Welcome to Tonic's🤖SuperAGI/SAM🚀"
7
+ description = """SAM is an Agentic-Native LLM that **excels at complex reasoning**.
8
+ You can also use [🤖SuperAGI/SAM](https://huggingface.co/SuperAGI/SAM) by cloning this space. 🧬🔬🔍 Simply click here: <a style="display:inline-block" href="https://huggingface.co/spaces/Tonic/superagi-sam?duplicate=true"><img src="https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14" alt="Duplicate Space"></a></h3>
9
+ Join us : 🌟TeamTonic🌟 is always making cool demos! Join our active builder's🛠️community 👻 [![Join us on Discord](https://img.shields.io/discord/1109943800132010065?label=Discord&logo=discord&style=flat-square)](https://discord.gg/GWpVpekp) On 🤗Huggingface: [TeamTonic](https://huggingface.co/TeamTonic) & [MultiTransformer](https://huggingface.co/MultiTransformer) On 🌐Github: [Tonic-AI](https://github.com/tonic-ai) & contribute to 🌟 [EasyAGI](https://github.com/tonic-ai/EasyAGI) 🤗Big thanks to Ythe folks at huggingface for the ZeroGPU 🤗
10
+ To contribute to this space make a PR with a new example or cool new use-case for this one 🤗
11
+ """
12
 
13
+ 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"]]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
 
15
  model_id = "SuperAGI/SAM"
 
16
  tokenizer = AutoTokenizer.from_pretrained(model_id)
17
+ model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, device_map="auto")
 
18
 
19
+ @spaces.GPU
20
+ def generate_response(formatted_input):
21
+ inputs = tokenizer(formatted_input, return_tensors="pt")
22
+ inputs = {k: v.to("cuda") for k, v in inputs.items()}
23
 
24
+ # Generate a response using the model
25
+ output = model.generate(**inputs, max_length=512, pad_token_id=tokenizer.eos_token_id)
26
 
27
+ return tokenizer.decode(output[0], skip_special_tokens=True)
28
 
29
  class ChatBot:
30
  def __init__(self):
 
31
  self.history = []
32
 
33
+ def predict(self, example_instruction, example_answer, user_input, system_prompt):
34
+ formatted_input = f"<s> [INST] {example_instruction} [/INST] {example_answer}</s> [INST] {system_prompt} {user_input} [/INST]"
35
+ return generate_response(formatted_input)
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  bot = ChatBot()
38
 
 
 
 
 
 
39
  def main():
40
  with gr.Blocks() as demo:
41
+
42
  gr.Markdown(title)
43
  gr.Markdown(description)
44
  with gr.Row():
 
60
  demo.launch()
61
 
62
  if __name__ == "__main__":
63
+ main()