File size: 3,730 Bytes
138bd73
6e4750f
bc93368
 
 
6e4750f
 
 
5765357
6e4750f
 
bc93368
6e4750f
bc93368
 
e32564e
6e4750f
bc93368
6e4750f
 
 
 
bc93368
6e4750f
 
bc93368
6e4750f
bc93368
 
 
 
 
6e4750f
 
 
bc93368
 
 
 
 
6e4750f
bc93368
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e4750f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import spaces
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
import gradio as gr

title = "# 🚀👋🏻Welcome to Tonic's🤖SuperAGI/SAM🚀"
description = """SAM is an Agentic-Native LLM that **excels at complex reasoning**. 
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> 
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 🌟 [DataTonic](https://github.com/Tonic-AI/DataTonic) 🤗Big thanks to Yuvi Sharma and all the folks at huggingface for the community grant 🤗
To contribute to this space make a PR with a new example or cool new use-case for this one 🤗
"""

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"]]

model_id = "SuperAGI/SAM"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, device_map="auto")

@spaces.GPU
def generate_response(formatted_input):
    inputs = tokenizer(formatted_input, return_tensors="pt")
    inputs = {k: v.to("cuda") for k, v in inputs.items()}

    # Generate a response using the model
    output = model.generate(**inputs, max_length=512, pad_token_id=tokenizer.eos_token_id)

    return tokenizer.decode(output[0], skip_special_tokens=True)

class ChatBot:
    def __init__(self):
        self.history = []

    def predict(self, example_instruction, example_answer, user_input, system_prompt):
        formatted_input = f"<s> [INST] {example_instruction} [/INST] {example_answer}</s> [INST] {system_prompt} {user_input} [/INST]"
        return generate_response(formatted_input)

bot = ChatBot()

def main():
    with gr.Blocks() as demo:

        gr.Markdown(title)
        gr.Markdown(description)
        with gr.Row():
            with gr.Column():
                example_instruction = gr.Textbox(label="Example Instruction")
                example_answer = gr.Textbox(label="Example Answer")
            with gr.Column():
                user_input = gr.Textbox(label="Your Question")
                system_prompt = gr.Textbox(label="System Prompt", value="You are an expert medical analyst:")
        submit_btn = gr.Button("Submit")
        output = gr.Textbox(label="Response")

        submit_btn.click(
            fn=bot.predict,
            inputs=[example_instruction, example_answer, user_input, system_prompt],
            outputs=output
        )

    demo.launch()

if __name__ == "__main__":
    main()