| import streamlit as st |
| from langchain import HuggingFaceHub |
| import os |
| from langchain import PromptTemplate, LLMChain |
|
|
|
|
| def get_llm(): |
|
|
| huggingfacehub_api_token = os.environ['HF_TOKEN'] |
|
|
| |
| repo_id = "Salvatale/Llama-2-7b-chat-finetune" |
| llm = HuggingFaceHub(huggingfacehub_api_token=huggingfacehub_api_token, |
| repo_id=repo_id, |
| model_kwargs={"temperature":0.5,"max_new_tokens":5000}) |
| return llm |
|
|
| def get_prompt(): |
|
|
| template = """ |
| You are a helpful AI assistant and provide the answer for the question asked politely. |
| |
| {question} |
| |
| Answer: |
| """ |
|
|
| prompt = PromptTemplate(template=template,input_variables=["question"]) |
|
|
| return prompt |
|
|
|
|
| def main(): |
| |
| |
| |
|
|
| llm = get_llm() |
| prompt = get_prompt() |
|
|
| llm_chain = LLMChain(prompt=prompt, llm=llm, verbose=True) |
|
|
| st.header("Chatbot") |
|
|
| user_input = st.text_input("You: ", "") |
| if user_input: |
| response = str(llm_chain.run(user_input)) |
| raws = response.splitlines() |
|
|
| new_text = '\n'.join(raws[7:]) |
| |
| st.text_area("Chatbot:", value=new_text) |
|
|
| if __name__ == "__main__": |
| main() |
|
|