Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,55 +1,29 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
-
import torch
|
4 |
-
import os
|
5 |
from memory import update_memory, check_memory
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
# model_name = "./MoinRomanticBot" # Uncomment if using local model folder
|
17 |
-
|
18 |
-
# β
Load Model & Tokenizer with Hugging Face Authentication
|
19 |
-
HF_TOKEN = os.getenv("HF_TOKEN") # Use token if model is private
|
20 |
-
|
21 |
-
try:
|
22 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name, token=HF_TOKEN)
|
23 |
-
model = AutoModelForCausalLM.from_pretrained(
|
24 |
-
model_name,
|
25 |
-
token=HF_TOKEN,
|
26 |
-
torch_dtype=torch.float16,
|
27 |
-
device_map="auto"
|
28 |
-
)
|
29 |
-
except Exception as e:
|
30 |
-
print(f"β Error loading model: {e}")
|
31 |
-
exit()
|
32 |
-
|
33 |
-
# β
Function to Generate Response with Memory
|
34 |
def chatbot(input_text):
|
35 |
memory_response = check_memory(input_text)
|
36 |
if memory_response:
|
37 |
return memory_response
|
38 |
-
|
39 |
-
prompt = f"{personality}\
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
outputs = model.generate(**inputs, max_length=150)
|
44 |
-
|
45 |
-
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
46 |
update_memory(input_text, response)
|
47 |
-
|
48 |
return response
|
49 |
|
50 |
-
# β
Gradio Interface
|
51 |
iface = gr.Interface(fn=chatbot, inputs="text", outputs="text", title="MoinRomanticBot")
|
52 |
|
53 |
-
# β
Launch App
|
54 |
if __name__ == "__main__":
|
55 |
iface.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
|
|
|
|
3 |
from memory import update_memory, check_memory
|
4 |
|
5 |
+
with open("persona.txt", "r", encoding="utf-8") as f:
|
6 |
+
personality = f.read()
|
7 |
+
|
8 |
+
model_name = "TheBloke/Pygmalion-7B-GPTQ"
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
10 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", trust_remote_code=True)
|
11 |
+
|
12 |
+
generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
13 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
def chatbot(input_text):
|
15 |
memory_response = check_memory(input_text)
|
16 |
if memory_response:
|
17 |
return memory_response
|
18 |
+
|
19 |
+
prompt = f"{personality}\nUser: {input_text}\nAI:"
|
20 |
+
outputs = generator(prompt, max_length=200, do_sample=True, temperature=0.7, top_p=0.9)
|
21 |
+
response = outputs[0]["generated_text"].split("AI:")[-1].strip()
|
22 |
+
|
|
|
|
|
|
|
23 |
update_memory(input_text, response)
|
|
|
24 |
return response
|
25 |
|
|
|
26 |
iface = gr.Interface(fn=chatbot, inputs="text", outputs="text", title="MoinRomanticBot")
|
27 |
|
|
|
28 |
if __name__ == "__main__":
|
29 |
iface.launch(server_name="0.0.0.0", server_port=7860)
|