Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
@@ -1,63 +1,55 @@
|
|
1 |
-
|
2 |
-
|
3 |
|
|
|
|
|
|
|
|
|
4 |
"""
|
5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
6 |
-
"""
|
7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
if val[1]:
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
|
26 |
-
|
|
|
27 |
|
28 |
-
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
|
39 |
-
|
40 |
-
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
respond,
|
47 |
-
additional_inputs=[
|
48 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
49 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
50 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
51 |
-
gr.Slider(
|
52 |
-
minimum=0.1,
|
53 |
-
maximum=1.0,
|
54 |
-
value=0.95,
|
55 |
-
step=0.05,
|
56 |
-
label="Top-p (nucleus sampling)",
|
57 |
-
),
|
58 |
-
],
|
59 |
-
)
|
60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
|
|
62 |
if __name__ == "__main__":
|
63 |
-
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""app.ipynb
|
3 |
|
4 |
+
Automatically generated by Colab.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1qIFntwH-_zF7GkQbgjKoXMXnQpZ4HVse
|
8 |
"""
|
|
|
|
|
|
|
9 |
|
10 |
+
import gradio as gr
|
11 |
+
import streamlit as st
|
12 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
13 |
+
|
14 |
+
# Load the base model
|
15 |
+
base_model_name = "Preetham04/sentiment-analysis"
|
16 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model_name)
|
17 |
+
model = AutoModelForSequenceClassification.from_pretrained(base_model_name)
|
18 |
|
19 |
+
# Load the adapter configuration and model files
|
20 |
+
adapter_config_path = "config.json"
|
21 |
+
adapter_model_path = "model.safetensors"
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
+
# Load the adapter into the model
|
24 |
+
adapter_name = "custom_adapter" # Define your adapter name
|
25 |
+
model.load_adapter(adapter_config_path, model_file=adapter_model_path, load_as=adapter_name)
|
|
|
|
|
26 |
|
27 |
+
# Activate the adapter
|
28 |
+
model.set_active_adapters(adapter_name)
|
29 |
|
30 |
+
st.title("🤖 Chatbot with Adapter-Enhanced Model")
|
31 |
+
st.write("Interact with your custom adapter-enhanced model. Type a message and get responses!")
|
32 |
|
33 |
+
# Initialize or retrieve the chat history
|
34 |
+
if 'history' not in st.session_state:
|
35 |
+
st.session_state['history'] = []
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
+
# Initialize Gradio
|
38 |
+
chatbot = Gradio(model=model, tokenizer=tokenizer)
|
39 |
|
40 |
+
# Define responses for greetings
|
41 |
+
@chatbot.on_event("welcome")
|
42 |
+
def welcome_handler(payload):
|
43 |
+
return "Welcome! Type a message and get responses from the chatbot."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
+
# Define responses for user messages
|
46 |
+
@chatbot.on_message
|
47 |
+
def message_handler(payload):
|
48 |
+
user_input = payload["message"]
|
49 |
+
response = chatbot.generate_response(user_input)
|
50 |
+
return response
|
51 |
|
52 |
+
# Run Gradio
|
53 |
if __name__ == "__main__":
|
54 |
+
chatbot.run()
|
55 |
+
|