Spaces:
Paused
Paused
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") | |
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() | |