Update app.py
Browse files
app.py
CHANGED
@@ -1,39 +1,29 @@
|
|
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
from llamafactory.chat import ChatModel
|
4 |
-
import gradio as gr
|
5 |
-
|
6 |
-
# Step 1: Load your model
|
7 |
-
args = dict(
|
8 |
-
model_name_or_path="unsloth/llama-3-8b-Instruct-bnb-4bit",
|
9 |
-
adapter_name_or_path="enzer1992/AI-Guru",
|
10 |
-
template="llama3",
|
11 |
-
finetuning_type="lora",
|
12 |
-
quantization_bit=4,
|
13 |
-
)
|
14 |
-
|
15 |
-
chat_model = ChatModel(args)
|
16 |
-
|
17 |
-
# Force the model to CPU
|
18 |
-
device = torch.device("cpu")
|
19 |
-
chat_model.model.to(device)
|
20 |
-
|
21 |
-
# Step 2: Create a function for chatting
|
22 |
-
def chat(user_input, history):
|
23 |
-
messages = history + [{"role": "user", "content": user_input}]
|
24 |
-
response = ""
|
25 |
-
for new_text in chat_model.stream_chat(messages):
|
26 |
-
response += new_text
|
27 |
-
history.append({"role": "user", "content": user_input})
|
28 |
-
history.append({"role": "assistant", "content": response})
|
29 |
-
return response, history
|
30 |
-
|
31 |
-
# Step 3: Create a simple interface
|
32 |
-
iface = gr.Interface(
|
33 |
-
fn=chat,
|
34 |
-
inputs=[gr.Textbox(label="Your Message"), gr.State()],
|
35 |
-
outputs=[gr.Textbox(label="AI Response"), gr.State()],
|
36 |
-
title="AI Guru Chatbot"
|
37 |
-
)
|
38 |
-
|
39 |
-
iface.launch()
|
|
|
1 |
|
2 |
+
from transformers import AutoProcessor, AutoModelForImageTextToText
|
3 |
+
|
4 |
+
# Step 1: Load the model and processor from Hugging Face
|
5 |
+
processor = AutoProcessor.from_pretrained("enzer1992/AI-Guru")
|
6 |
+
model = AutoModelForImageTextToText.from_pretrained("enzer1992/AI-Guru")
|
7 |
+
|
8 |
+
# Step 2: Function to interact with the model
|
9 |
+
def chat_with_model(input_text):
|
10 |
+
# Process the input text
|
11 |
+
inputs = processor(input_text, return_tensors="pt")
|
12 |
+
|
13 |
+
# Generate the model's response
|
14 |
+
outputs = model.generate(**inputs)
|
15 |
+
|
16 |
+
# Decode and return the response
|
17 |
+
response = processor.decode(outputs[0], skip_special_tokens=True)
|
18 |
+
return response
|
19 |
+
|
20 |
+
# Step 3: Chat loop
|
21 |
+
print("Chat with the model! Type 'exit' to end.")
|
22 |
+
while True:
|
23 |
+
user_input = input("You: ")
|
24 |
+
if user_input.lower() == "exit":
|
25 |
+
break
|
26 |
+
|
27 |
+
response = chat_with_model(user_input)
|
28 |
+
print(f"Model: {response}")
|
29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|