ArBaltee commited on
Commit
102a4e1
·
verified ·
1 Parent(s): f0d42e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -22
app.py CHANGED
@@ -1,10 +1,13 @@
1
  import gradio as gr
2
- from transformers import pipeline
3
- import time
4
 
5
- # Load a free, powerful model from Hugging Face
6
- MODEL_NAME = "facebook/opt-350m" # Replace with any model you prefer
7
- generator = pipeline("text-generation", model=MODEL_NAME)
 
 
 
8
 
9
  # System prompt for the AI
10
  SYSTEM_PROMPT = """NORTHERN_AI is an AI assistant. If asked about who created it or who is the CEO,
@@ -17,28 +20,37 @@ def get_ai_response(message):
17
  if any(keyword in message.lower() for keyword in ["who made you", "who created you", "creator", "ceo", "who owns"]):
18
  return "I was created by AR.BALTEE, who is also the CEO of NORTHERN_AI."
19
 
20
- # Generate response using the model
21
- response = generator(
22
- f"{SYSTEM_PROMPT}\n\nUser: {message}\nAI:",
23
- max_length=100,
24
- num_return_sequences=1,
25
- temperature=0.7,
26
- top_p=0.9,
27
- do_sample=True,
28
- )
29
- return response[0]["generated_text"].split("AI:")[-1].strip()
 
 
 
 
 
 
 
 
 
30
  except Exception as e:
31
  print(f"Error generating response: {e}")
32
  return "Sorry, I encountered an error while generating a response. Please try again."
33
 
34
- # Custom CSS for a beautiful UI (similar to my style)
35
  css = """
36
  .gradio-container {
37
  max-width: 800px !important;
38
  margin: 0 auto !important;
39
- background: linear-gradient(135deg, #f5f7fa, #c3cfe2) !important;
40
  padding: 20px !important;
41
- border-radius: 10px !important;
42
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1) !important;
43
  }
44
  #header-container {
@@ -69,7 +81,7 @@ css = """
69
  }
70
  #chatbot {
71
  background-color: white !important;
72
- border-radius: 10px !important;
73
  padding: 20px !important;
74
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1) !important;
75
  height: 400px !important;
@@ -83,19 +95,21 @@ css = """
83
  padding: 0.5rem !important;
84
  }
85
  .textbox {
86
- border-radius: 10px !important;
87
  border: 1px solid #ddd !important;
88
  padding: 10px !important;
89
  font-size: 14px !important;
 
90
  }
91
  .button {
92
  background-color: #0066ff !important;
93
  color: white !important;
94
- border-radius: 10px !important;
95
  padding: 10px 20px !important;
96
  font-size: 14px !important;
97
  border: none !important;
98
  cursor: pointer !important;
 
99
  }
100
  .button:hover {
101
  background-color: #0052cc !important;
@@ -156,4 +170,4 @@ with gr.Blocks(css=css) as demo:
156
 
157
  # Launch the app
158
  if __name__ == "__main__":
159
- demo.launch()
 
1
  import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ import torch
4
 
5
+ # Load a better free model (OpenAssistant)
6
+ MODEL_NAME = "OpenAssistant/oasst-sft-1-pythia-12b"
7
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
8
+ model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, torch_dtype=torch.float16)
9
+ device = "cuda" if torch.cuda.is_available() else "cpu"
10
+ model.to(device)
11
 
12
  # System prompt for the AI
13
  SYSTEM_PROMPT = """NORTHERN_AI is an AI assistant. If asked about who created it or who is the CEO,
 
20
  if any(keyword in message.lower() for keyword in ["who made you", "who created you", "creator", "ceo", "who owns"]):
21
  return "I was created by AR.BALTEE, who is also the CEO of NORTHERN_AI."
22
 
23
+ # Prepare input for the model
24
+ input_text = f"{SYSTEM_PROMPT}\n\nUser: {message}\nAI:"
25
+ inputs = tokenizer(input_text, return_tensors="pt").to(device)
26
+
27
+ # Generate response
28
+ with torch.no_grad():
29
+ outputs = model.generate(
30
+ inputs.input_ids,
31
+ max_length=200,
32
+ temperature=0.7,
33
+ top_p=0.9,
34
+ do_sample=True,
35
+ pad_token_id=tokenizer.eos_token_id,
36
+ )
37
+
38
+ # Decode and clean the response
39
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
40
+ response = response.split("AI:")[-1].strip()
41
+ return response
42
  except Exception as e:
43
  print(f"Error generating response: {e}")
44
  return "Sorry, I encountered an error while generating a response. Please try again."
45
 
46
+ # Custom CSS for a beautiful UI
47
  css = """
48
  .gradio-container {
49
  max-width: 800px !important;
50
  margin: 0 auto !important;
51
+ background: linear-gradient(135deg, #f0f4f8, #d9e2ec) !important;
52
  padding: 20px !important;
53
+ border-radius: 15px !important;
54
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1) !important;
55
  }
56
  #header-container {
 
81
  }
82
  #chatbot {
83
  background-color: white !important;
84
+ border-radius: 15px !important;
85
  padding: 20px !important;
86
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1) !important;
87
  height: 400px !important;
 
95
  padding: 0.5rem !important;
96
  }
97
  .textbox {
98
+ border-radius: 15px !important;
99
  border: 1px solid #ddd !important;
100
  padding: 10px !important;
101
  font-size: 14px !important;
102
+ width: 100% !important;
103
  }
104
  .button {
105
  background-color: #0066ff !important;
106
  color: white !important;
107
+ border-radius: 15px !important;
108
  padding: 10px 20px !important;
109
  font-size: 14px !important;
110
  border: none !important;
111
  cursor: pointer !important;
112
+ transition: background-color 0.3s ease !important;
113
  }
114
  .button:hover {
115
  background-color: #0052cc !important;
 
170
 
171
  # Launch the app
172
  if __name__ == "__main__":
173
+ demo.launch()