ArBaltee commited on
Commit
2dc5dda
·
verified ·
1 Parent(s): d7dee7d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +143 -94
app.py CHANGED
@@ -1,112 +1,161 @@
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 by AR.BALTEE") as demo:
54
- gr.Markdown("# NORTHERN_AI by AR.BALTEE")
55
- gr.Markdown("### Your AI assistant powered by open-source models")
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()
112
 
 
1
  import os
2
  import gradio as gr
3
+ import time
4
+ import random
5
 
6
  # Configuration
7
+ MODEL_NAME = "facebook/opt-350m" # Free, open model without API key requirements
8
+ SYSTEM_PROMPT = """NORTHERN_AI is an AI assistant. If asked about who created it or who is the CEO,
9
+ it should respond that it was created by AR.BALTEE who is also the CEO."""
10
 
11
+ # For demonstration purposes, we'll use a simpler model approach
12
+ # that doesn't require an API key but still provides responses
13
+ def get_ai_response(message):
14
+ """Generate a response using a local model"""
15
+ # Check if asking about creator/CEO
16
+ if any(keyword in message.lower() for keyword in ["who made you", "who created you", "creator", "ceo", "who owns"]):
17
+ return "I was created by AR.BALTEE, who is also the CEO of NORTHERN_AI."
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
+ # Simple response generation (replace this with actual model inference)
20
+ responses = [
21
+ "I understand your question about {}. Based on my knowledge, I would say that...",
22
+ "Thanks for asking about {}. Here's what I know on this topic...",
23
+ "Regarding {}, I can provide some information...",
24
+ "I've analyzed your question about {} and here's my perspective...",
25
+ "When it comes to {}, there are several important points to consider..."
26
+ ]
27
 
28
+ # Extract key topic from message
29
+ topic = message.split()[0] if len(message.split()) > 0 else "that"
30
+
31
+ # Add a slight delay to simulate processing
32
+ time.sleep(0.5)
33
+
34
+ return random.choice(responses).format(topic)
35
+
36
+ # Define theme and styling
37
+ theme = gr.themes.Soft(
38
+ primary_hue="blue",
39
+ secondary_hue="blue",
40
+ neutral_hue="gray",
41
+ spacing_size=gr.themes.sizes.spacing_sm,
42
+ radius_size=gr.themes.sizes.radius_sm,
43
+ text_size=gr.themes.sizes.text_md
44
+ ).set(
45
+ body_text_color="black",
46
+ body_background_fill="white",
47
+ button_primary_background_fill="rgb(51, 102, 255)",
48
+ button_primary_background_fill_hover="rgb(41, 82, 204)",
49
+ button_primary_text_color="white",
50
+ button_primary_text_color_hover="white",
51
+ chatbot_code_background_color="rgba(0, 0, 0, 0.05)",
52
+ chatbot_code_foreground_color="rgb(51, 51, 51)"
53
+ )
54
+
55
+ # Load custom CSS
56
+ css = """
57
+ #chat-container {
58
+ max-width: 800px;
59
+ margin: 0 auto;
60
+ }
61
+ #header {
62
+ display: flex;
63
+ align-items: center;
64
+ margin-bottom: 16px;
65
+ padding: 0 8px;
66
+ }
67
+ #logo {
68
+ width: 32px;
69
+ height: 32px;
70
+ margin-right: 8px;
71
+ }
72
+ #title {
73
+ margin: 0;
74
+ font-size: 18px;
75
+ font-weight: 600;
76
+ }
77
+ .chatbot-message {
78
+ padding: 8px 12px;
79
+ border-radius: 8px;
80
+ margin-bottom: 8px;
81
+ }
82
+ .user-message {
83
+ background-color: #f0f0f0;
84
+ }
85
+ .bot-message {
86
+ background-color: #e6f7ff;
87
+ }
88
+ #input-container {
89
+ margin-top: 8px;
90
+ }
91
+ #footer {
92
+ font-size: 12px;
93
+ color: #666;
94
+ text-align: center;
95
+ margin-top: 16px;
96
+ }
97
+ """
98
 
99
  # Create Gradio interface
100
+ with gr.Blocks(theme=theme, css=css) as demo:
101
+ with gr.Column(elem_id="chat-container"):
102
+ with gr.Row(elem_id="header"):
103
+ gr.HTML("""
104
+ <div id="logo" style="background-color: #0066ff; color: white; border-radius: 50%; width: 32px; height: 32px; display: flex; align-items: center; justify-content: center; font-weight: bold;">N</div>
105
+ """)
106
+ gr.HTML('<h1 id="title">NORTHERN_AI</h1>')
107
+
108
+ chatbot = gr.Chatbot(
109
+ elem_id="chatbox",
110
+ bubble_full_width=False,
111
+ height=400,
112
+ show_label=False,
113
+ avatar_images=("https://api.dicebear.com/7.x/avataaars/svg?seed=user", "https://api.dicebear.com/7.x/avataaars/svg?seed=northern")
114
+ )
115
+
116
+ with gr.Row(elem_id="input-container"):
117
  msg = gr.Textbox(
118
+ placeholder="Message NORTHERN_AI...",
119
+ show_label=False,
120
+ container=False
121
  )
122
+ submit_btn = gr.Button("Send", variant="primary")
 
 
 
 
 
 
 
 
 
 
123
 
124
+ gr.HTML(
125
+ """<div id="footer">Powered by open-source technology</div>""",
126
+ elem_id="footer"
127
+ )
128
 
129
+ # State for tracking conversation
130
+ state = gr.State([])
 
 
 
 
131
 
132
+ # Functions
133
+ def respond(message, chat_history):
134
+ if message == "":
135
+ return "", chat_history
136
+
137
+ # Add user message to history
138
+ chat_history.append((message, None))
139
+
140
+ try:
141
+ # Generate response
142
+ bot_message = get_ai_response(message)
143
+
144
+ # Update last message with bot response
145
+ chat_history[-1] = (message, bot_message)
146
+
147
+ return "", chat_history
148
+ except Exception as e:
149
+ print(f"Error generating response: {e}")
150
+ # Remove failed message attempt
151
+ chat_history.pop()
152
+ return "", chat_history
153
+
154
+ # Set up event handlers
155
+ msg.submit(respond, [msg, state], [msg, chatbot])
156
+ submit_btn.click(respond, [msg, state], [msg, chatbot])
157
 
158
+ # Launch the app
159
  if __name__ == "__main__":
160
  demo.launch()
161