CoolFace
Apppublic

CosmoAI/ChitChat

sourceHugging Faceopenrailupdated 3y agoView on Hugging Face
2likes
app.py123 linesDownload Raw Back to root
1import gradio2# from transformers import pipeline3from transformers import AutoTokenizer, AutoModelForCausalLM4import os5 6os.getenv("HF_TOKEN")7 8# Initialize the Hugging Face model9# model = pipeline(model='google/flan-t5-base')10tokenizer = AutoTokenizer.from_pretrained("google/gemma-7b", use_auth_token=True)11model = AutoModelForCausalLM.from_pretrained("google/gemma-7b", use_auth_token=True)12 13 14# Define the chatbot function15def chatbot(input_text):16 17    prompt = f"Give the answer of the given input in context from the bhagwat geeta. give suggestions to user which are based upon the meanings of shlok in bhagwat geeta, input = {input_text}"18    # Generate a response from the Hugging Face model19    # response = model(prompt, max_length=250, do_sample=True)[0]['generated_text'].strip()20    input_text = "Write me a poem about Machine Learning."21    input_ids = tokenizer(prompt, return_tensors="pt")22 23    outputs = model.generate(**input_ids)24  25    # Return the bot response26    return outputs27 28# Define the Gradio interface29gradio_interface = gradio.Interface(30    fn=chatbot,31    inputs='text',32    outputs='text',33    title='Chatbot',34    description='A weird chatbot conversations experience.',35    examples=[36        ['Hi, how are you?']37    ]38)39 40# Launch the Gradio interface41gradio_interface.launch()42 43 44 45 46 47# from dotenv import load_dotenv48# from langchain import HuggingFaceHub, LLMChain49# from langchain import PromptTemplates50# import gradio51 52# load_dotenv()53# os.getenv('HF_API')54 55# hub_llm = HuggingFaceHub(repo_id='facebook/blenderbot-400M-distill')56 57# prompt = prompt_templates(58#     input_variable = ["question"],59#     template = "Answer is: {question}"60# )61 62# hub_chain = LLMChain(prompt=prompt, llm=hub_llm, verbose=True)63 64 65 66 67 68# Sample code for AI language model interaction69# from transformers import GPT2Tokenizer, GPT2LMHeadModel70# import gradio71 72 73# def simptok(data):74#     # Load pre-trained model and tokenizer (using the transformers library)75#     model_name = "gpt2"76#     tokenizer = GPT2Tokenizer.from_pretrained(model_name)77#     model = GPT2LMHeadModel.from_pretrained(model_name)78    79#     # User input80#     user_input = data81    82#     # Tokenize input83#     input_ids = tokenizer.encode(user_input, return_tensors="pt")84    85#     # Generate response86#     output = model.generate(input_ids, max_length=50, num_return_sequences=1)87#     response = tokenizer.decode(output[0], skip_special_tokens=True)88#     return response89 90 91# def responsenew(data):92#     return simptok(data)93 94 95# from hugchat import hugchat96# import gradio as gr97# import time98 99# # Create a chatbot connection100# chatbot = hugchat.ChatBot(cookie_path="cookies.json")101 102# # New a conversation (ignore error)103# id = chatbot.new_conversation()104# chatbot.change_conversation(id)105 106 107# def get_answer(data):108#     return chatbot.chat(data)109 110# gradio_interface = gr.Interface(111#   fn = get_answer,112#   inputs = "text",113#   outputs = "text"114# )115# gradio_interface.launch()116 117# gradio_interface = gradio.Interface(118#   fn = responsenew,119#   inputs = "text",120#   outputs = "text"121# )122# gradio_interface.launch()123