KvrParaskevi
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from langchain_core.pydantic_v1 import BaseModel, Field
|
3 |
+
from langchain.prompts import HumanMessagePromptTemplate, ChatPromptTemplate
|
4 |
+
from langchain.output_parsers import TextIteratorStreamer
|
5 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
6 |
+
|
7 |
+
# Load the Hugging Face model and tokenizer
|
8 |
+
model_name = "KvrParaskevi/Llama-2-7b-Hotel-Booking-Model"
|
9 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
11 |
+
|
12 |
+
# Define the Langchain chatbot function
|
13 |
+
def chatbot(message, history):
|
14 |
+
# Create a Langchain prompt template
|
15 |
+
prompt_template = HumanMessagePromptTemplate.from_message(message)
|
16 |
+
# Create a Langchain chat prompt template
|
17 |
+
chat_prompt_template = ChatPromptTemplate.from_messages([prompt_template])
|
18 |
+
# Use the Langchain TextIteratorStreamer to generate responses
|
19 |
+
streamer = TextIteratorStreamer(model, tokenizer, chat_prompt_template)
|
20 |
+
response = streamer.generate()
|
21 |
+
return response
|
22 |
+
|
23 |
+
# Create a Gradio chatbot interface
|
24 |
+
with gr.Blocks() as demo:
|
25 |
+
chatbot_interface = gr.Chatbot()
|
26 |
+
msg = gr.Textbox()
|
27 |
+
clear = gr.Button("Clear")
|
28 |
+
|
29 |
+
# Define the chatbot function as a Gradio interface
|
30 |
+
demo.chatbot_interface = gr.Interface(
|
31 |
+
fn=chatbot,
|
32 |
+
inputs="text",
|
33 |
+
outputs="text",
|
34 |
+
title="Langchain Chatbot",
|
35 |
+
description="A simple chatbot using Langchain and Hugging Face"
|
36 |
+
)
|
37 |
+
|
38 |
+
# Launch the Gradio app
|
39 |
+
demo.launch()
|