ArBaltee commited on
Commit
6496baa
·
verified ·
1 Parent(s): 9fc9cb0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +141 -93
app.py CHANGED
@@ -1,111 +1,159 @@
1
- import os
2
  import gradio as gr
3
- from huggingface_hub import InferenceClient
 
4
 
5
- # Configuration
6
- MODEL_NAME = "EleutherAI/gpt-neo-125M" # A relatively small model
7
- HF_API_TOKEN = os.environ.get("HF_API_TOKEN", "") # Your Hugging Face API token
8
 
9
- # System prompt defining your AI assistant's personality
10
- SYSTEM_PROMPT = """NORTHERN_AI is a helpful AI assistant created by AR.BALTEE.
11
- It aims to provide accurate and helpful information to users' questions.
12
- NORTHERN_AI is friendly, concise, and knowledgeable."""
13
 
14
- # Initialize the inference client
15
- client = None
16
- if HF_API_TOKEN:
17
  try:
18
- client = InferenceClient(token=HF_API_TOKEN)
19
- print("Successfully initialized Hugging Face Inference Client")
20
- except Exception as e:
21
- print(f"Error initializing Inference Client: {e}")
22
- else:
23
- print("Warning: HF_API_TOKEN not set. Using limited functionality.")
24
 
25
- def generate_response(user_input):
26
- """Generate a response from the model based on user input"""
27
- if not user_input.strip():
28
- return "Please enter a message to get a response from NORTHERN_AI."
29
-
30
- prompt = f"{SYSTEM_PROMPT}\n\nUser: {user_input}\nNORTHERN_AI:"
31
-
32
- try:
33
- if client:
34
- # Use Hugging Face's Inference API
35
- response = client.text_generation(
36
- prompt,
37
- model=MODEL_NAME,
38
- max_new_tokens=150,
39
- temperature=0.7,
40
- top_p=0.95,
41
- repetition_penalty=1.1
42
- )
43
- # Extract just the assistant's response
44
- result = response.split("NORTHERN_AI:")[-1].strip()
45
- return result
46
- else:
47
- return "AI service is currently unavailable. Please check your API token configuration."
48
  except Exception as e:
49
  print(f"Error generating response: {e}")
50
- return f"Sorry, I encountered an error while generating a response. Please try again later."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
  # Create Gradio interface
53
- with gr.Blocks(title="NORTHERN_AI") as demo:
54
- gr.Markdown("# NORTHERN_AI by AR.BALTEE")
55
- gr.Markdown("### Your AI assistant powered by NORTHERN 0.1")
56
-
57
- with gr.Row():
58
- with gr.Column(scale=4):
59
- chatbot = gr.Chatbot(label="Conversation", height=400)
 
 
 
 
60
  msg = gr.Textbox(
61
- label="Type your message",
62
- placeholder="Ask NORTHERN_AI something...",
63
- lines=2
 
64
  )
65
-
66
- with gr.Row():
67
- submit = gr.Button("Send", variant="primary")
68
- clear = gr.Button("Clear", variant="secondary")
69
-
70
- # Keep track of conversation history
71
- conversation_history = gr.State([])
72
-
73
- def user_input(message, history):
74
- if not message:
75
- return "", history
76
 
77
- # Get AI response
78
- ai_response = generate_response(message)
79
 
80
- # Update history
81
- history.append((message, ai_response))
82
- return "", history
83
-
84
- def clear_conversation():
85
- return [], []
86
 
87
- submit.click(
88
- user_input,
89
- inputs=[msg, conversation_history],
90
- outputs=[msg, chatbot]
91
- )
92
-
93
- msg.submit(
94
- user_input,
95
- inputs=[msg, conversation_history],
96
- outputs=[msg, chatbot]
97
- )
98
-
99
- clear.click(
100
- clear_conversation,
101
- outputs=[chatbot, conversation_history]
102
- )
103
-
104
- gr.HTML("""
105
- <div style="text-align: center; margin-top: 20px;">
106
- <p>Created by AR.BALTEE - NORTHERN_AI © 2025</p>
107
- </div>
108
- """)
 
 
 
 
109
 
 
110
  if __name__ == "__main__":
111
  demo.launch()
 
 
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,
11
+ it should respond that it was created by AR.BALTEE who is also the CEO."""
 
12
 
13
+ # Function to generate AI responses
14
+ def get_ai_response(message):
 
15
  try:
16
+ # Check if asking about creator/CEO
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 {
45
+ display: flex !important;
46
+ align-items: center !important;
47
+ margin-bottom: 1.5rem !important;
48
+ background-color: transparent !important;
49
+ padding: 0.5rem 1rem !important;
50
+ }
51
+ #logo {
52
+ background-color: #0066ff !important;
53
+ color: white !important;
54
+ border-radius: 50% !important;
55
+ width: 40px !important;
56
+ height: 40px !important;
57
+ display: flex !important;
58
+ align-items: center !important;
59
+ justify-content: center !important;
60
+ font-weight: bold !important;
61
+ margin-right: 10px !important;
62
+ font-size: 20px !important;
63
+ }
64
+ #title {
65
+ margin: 0 !important;
66
+ font-size: 24px !important;
67
+ font-weight: 600 !important;
68
+ color: #333 !important;
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;
76
+ overflow-y: auto !important;
77
+ }
78
+ #footer {
79
+ font-size: 12px !important;
80
+ color: #666 !important;
81
+ text-align: center !important;
82
+ margin-top: 1.5rem !important;
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;
102
+ }
103
+ """
104
 
105
  # Create Gradio interface
106
+ with gr.Blocks(css=css) as demo:
107
+ with gr.Column():
108
+ # Custom header
109
+ with gr.Row(elem_id="header-container"):
110
+ gr.HTML('<div id="logo">N</div>')
111
+ gr.HTML('<h1 id="title">NORTHERN_AI</h1>')
112
+
113
+ # Chat interface
114
+ chatbot = gr.Chatbot(elem_id="chatbot")
115
+
116
+ with gr.Row():
117
  msg = gr.Textbox(
118
+ placeholder="Message NORTHERN_AI...",
119
+ show_label=False,
120
+ container=False,
121
+ elem_classes="textbox"
122
  )
123
+ submit_btn = gr.Button("Send", elem_classes="button")
 
 
 
 
 
 
 
 
 
 
124
 
125
+ gr.HTML('<div id="footer">Powered by open-source technology</div>')
 
126
 
127
+ # State for tracking conversation
128
+ state = gr.State([])
 
 
 
 
129
 
130
+ # Functions
131
+ def respond(message, chat_history):
132
+ if message == "":
133
+ return "", chat_history
134
+
135
+ # Add user message to history
136
+ chat_history.append((message, None))
137
+
138
+ try:
139
+ # Generate response
140
+ bot_message = get_ai_response(message)
141
+
142
+ # Update last message with bot response
143
+ chat_history[-1] = (message, bot_message)
144
+
145
+ return "", chat_history
146
+ except Exception as e:
147
+ print(f"Error generating response: {e}")
148
+ # Remove failed message attempt
149
+ chat_history.pop()
150
+ # Return error message
151
+ return "", chat_history
152
+
153
+ # Set up event handlers
154
+ msg.submit(respond, [msg, state], [msg, chatbot])
155
+ submit_btn.click(respond, [msg, state], [msg, chatbot])
156
 
157
+ # Launch the app
158
  if __name__ == "__main__":
159
  demo.launch()