Falln87 commited on
Commit
7c6415e
1 Parent(s): 9688851

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -0
app.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
3
+
4
+ # Select the models you want to offer for chat
5
+ MODELS = [
6
+ "gpt2",
7
+ "distilgpt2",
8
+ "openai-gpt",
9
+ "openai-gpt-2",
10
+ "openai-gpt3",
11
+ ]
12
+
13
+ # Define the system prompt
14
+ SYSTEM_PROMPT = "You are a helpful assistant. Answer the user's questions as best as you can."
15
+
16
+ # Create a dictionary to store conversation history
17
+ conversation_history = {}
18
+
19
+ # Create a function to generate the chatbot response
20
+ def chatbot_response(input_text, model_name, system_prompt):
21
+ # Load the selected model and tokenizer
22
+ model = AutoModelForCausalLM.from_pretrained(model_name)
23
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
24
+
25
+ # Define the chatbot pipeline
26
+ chatbot_pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
27
+
28
+ # Prepare the input for the chatbot
29
+ inputs = tokenizer([SYSTEM_PROMPT + " " + input_text], return_tensors="pt")
30
+
31
+ # Generate a response from the chatbot
32
+ response = chatbot_pipe(inputs)
33
+
34
+ # Return the response
35
+ return response[0]['generated_text'].strip()
36
+
37
+ # Create a Gradio interface
38
+ interface = gr.Interface(
39
+ fn=chatbot_response,
40
+ inputs=[
41
+ gr.inputs.Textbox(label="User input"),
42
+ gr.inputs.Radio(choices=MODELS, label="Model"),
43
+ gr.inputs.Textbox(label="System prompt", value=SYSTEM_PROMPT),
44
+ ],
45
+ outputs="text",
46
+ title="Large Language Model Chatbot",
47
+ description="Chat with a large language model from the HuggingFace Transformers library.",
48
+ )
49
+
50
+ # Initialize the conversation history
51
+ for model in MODELS:
52
+ conversation_history[model] = []
53
+
54
+ # Define a function to update the conversation history
55
+ def update_history(history, new_message):
56
+ history.append(new_message)
57
+ return history
58
+
59
+ # Define a function to display the conversation history
60
+ def display_history(history):
61
+ return "\n".join(history)
62
+
63
+ # Create a Gradio block to display the conversation history
64
+ history_block = gr.Block(
65
+ label="Conversation History",
66
+ elem_id="history",
67
+ visible=False,
68
+ )
69
+
70
+ # Update the conversation history when a new message is sent
71
+ def update_history_on_message(history, model, new_message):
72
+ history = update_history(history, new_message)
73
+ conversation_history[model] = history
74
+ return history
75
+
76
+ # Display the conversation history
77
+ def display_history_on_message(history):
78
+ return display_history(history)
79
+
80
+ # Define event handlers for the Gradio interface
81
+ interface.change(history_block.update, [conversation_history], queue=False)
82
+ interface.submit(update_history_on_message, [conversation_history], [conversation_history], queue=False)
83
+ history_block.change(display_history_on_message, [conversation_history], queue=False)
84
+
85
+ # Launch the Gradio interface
86
+ interface.launch()