Spaces:
Runtime error
Runtime error
leungchunghong
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,47 @@
|
|
1 |
-
|
|
|
2 |
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
)
|
8 |
-
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
3 |
|
4 |
+
# Load the model and tokenizer from Hugging Face
|
5 |
+
model_name = "OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5" # Example model, replace with the desired model
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
7 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
8 |
+
|
9 |
+
# Create a pipeline for text generation
|
10 |
+
chat_pipeline = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
11 |
+
|
12 |
+
# Define the function that handles chat interaction
|
13 |
+
def chat_with_model(user_input, history=[]):
|
14 |
+
# Append the user input to the history
|
15 |
+
history.append({"role": "user", "content": user_input})
|
16 |
+
|
17 |
+
# Prepare the prompt from the history
|
18 |
+
prompt = "\n".join([f"{item['role']}: {item['content']}" for item in history])
|
19 |
+
|
20 |
+
# Generate a response
|
21 |
+
response = chat_pipeline(prompt, max_length=100, num_return_sequences=1)[0]['generated_text']
|
22 |
+
|
23 |
+
# Extract only the model's response
|
24 |
+
model_response = response[len(prompt):].strip()
|
25 |
+
|
26 |
+
# Append model response to history
|
27 |
+
history.append({"role": "assistant", "content": model_response})
|
28 |
+
|
29 |
+
return model_response, history
|
30 |
+
|
31 |
+
# Define the Gradio interface
|
32 |
+
iface = gr.Interface(
|
33 |
+
fn=chat_with_model,
|
34 |
+
inputs=[
|
35 |
+
gr.inputs.Textbox(label="User Input"),
|
36 |
+
gr.inputs.State(),
|
37 |
+
],
|
38 |
+
outputs=[
|
39 |
+
gr.outputs.Textbox(label="Chatbot Response"),
|
40 |
+
gr.outputs.State(),
|
41 |
+
],
|
42 |
+
title="HuggingChat Clone",
|
43 |
+
description="A simple chatbot interface using an open-source model from Hugging Face."
|
44 |
)
|
45 |
+
|
46 |
+
# Launch the interface
|
47 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|