Spaces:
Paused
Paused
Wenye He
commited on
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Choose your model – here we use GPT-2 as an example
|
| 6 |
+
model_name = "gpt2"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 9 |
+
|
| 10 |
+
def generate_response(user_input, chat_history):
|
| 11 |
+
"""
|
| 12 |
+
This function takes the user's input and current conversation history,
|
| 13 |
+
appends the input to the history, builds the conversation string, and
|
| 14 |
+
generates a response using the local LLM.
|
| 15 |
+
"""
|
| 16 |
+
if chat_history is None:
|
| 17 |
+
chat_history = []
|
| 18 |
+
|
| 19 |
+
# Append the user message to the conversation history.
|
| 20 |
+
chat_history.append(("User", user_input))
|
| 21 |
+
|
| 22 |
+
# Build a conversation string from the history.
|
| 23 |
+
conversation = ""
|
| 24 |
+
for speaker, message in chat_history:
|
| 25 |
+
conversation += f"{speaker}: {message}\n"
|
| 26 |
+
conversation += "AI:" # Signal for the model to generate AI's response
|
| 27 |
+
|
| 28 |
+
# Tokenize the input and generate a response.
|
| 29 |
+
input_ids = tokenizer.encode(conversation, return_tensors="pt")
|
| 30 |
+
output_ids = model.generate(
|
| 31 |
+
input_ids,
|
| 32 |
+
max_length=input_ids.shape[1] + 50, # Adjust max_length as needed
|
| 33 |
+
pad_token_id=tokenizer.eos_token_id
|
| 34 |
+
)
|
| 35 |
+
generated_text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 36 |
+
|
| 37 |
+
# Extract only the AI response (everything after the last "AI:" prompt).
|
| 38 |
+
ai_response = generated_text[len(conversation):].strip().split("\n")[0]
|
| 39 |
+
chat_history.append(("AI", ai_response))
|
| 40 |
+
|
| 41 |
+
# Return an empty string (to clear the input box) and updated chat history.
|
| 42 |
+
return "", chat_history
|
| 43 |
+
|
| 44 |
+
# Build the Gradio interface using Blocks for a flexible layout.
|
| 45 |
+
with gr.Blocks() as demo:
|
| 46 |
+
gr.Markdown("# Local LLM Chatbot")
|
| 47 |
+
|
| 48 |
+
# Chatbot display widget
|
| 49 |
+
chatbot = gr.Chatbot()
|
| 50 |
+
|
| 51 |
+
# Hidden state to hold the conversation history
|
| 52 |
+
state = gr.State([])
|
| 53 |
+
|
| 54 |
+
# Textbox for user input
|
| 55 |
+
txt = gr.Textbox(placeholder="Enter your message and press Enter")
|
| 56 |
+
|
| 57 |
+
# When the textbox is submitted, generate a response.
|
| 58 |
+
txt.submit(generate_response, [txt, state], [txt, chatbot])
|
| 59 |
+
|
| 60 |
+
# Launch the interface
|
| 61 |
+
demo.launch()
|