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: Duplicate Space 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" [INST] {example_instruction} [/INST] {example_answer} [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()