syedmoinms commited on
Commit
a1bbc6a
Β·
verified Β·
1 Parent(s): 8eef107

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -41
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
- # βœ… Load persona instructions
8
- try:
9
- with open("persona.txt", "r", encoding="utf-8") as f:
10
- personality = f.read()
11
- except FileNotFoundError:
12
- personality = "You are a romantic AI chatbot designed to chat with Moin."
13
-
14
- # βœ… Fix: Use Correct Model Name
15
- model_name = "syedmoinms/MoinRomanticBot" # Correct Hugging Face model path
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}\nMoin: {input_text}\nAI:"
40
- inputs = tokenizer(prompt, return_tensors="pt").to("cuda" if torch.cuda.is_available() else "cpu")
41
-
42
- with torch.no_grad():
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)