BikoRiko commited on
Commit
6ebd2d9
·
verified ·
1 Parent(s): 1a9677f

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. app.py +280 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,280 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ from typing import List, Dict, Optional
4
+
5
+ # Function to handle chat messages
6
+ def chat(message: str, history: List[Dict[str, str]], model_name: str, temperature: float, max_tokens: int) -> List[Dict[str, str]]:
7
+ """
8
+ Generate a response using the Hugging Face Inference API.
9
+
10
+ Args:
11
+ message: The user's current message
12
+ history: Chat history with previous messages
13
+ model_name: The Hugging Face model to use
14
+ temperature: Sampling temperature for generation
15
+ max_tokens: Maximum tokens to generate
16
+
17
+ Returns:
18
+ Updated chat history
19
+ """
20
+ try:
21
+ # Build the conversation context
22
+ conversation = []
23
+
24
+ # Add system message
25
+ conversation.append({
26
+ "role": "system",
27
+ "content": "You are a helpful, friendly AI assistant. Provide concise, accurate responses."
28
+ })
29
+
30
+ # Add conversation history
31
+ for user_msg, assistant_msg in history:
32
+ conversation.append({"role": "user", "content": user_msg})
33
+ if assistant_msg:
34
+ conversation.append({"role": "assistant", "content": assistant_msg})
35
+
36
+ # Add current message
37
+ conversation.append({"role": "user", "content": message})
38
+
39
+ # Make API call to Hugging Face
40
+ headers = {"Authorization": f"Bearer {os.environ.get('HF_TOKEN', '')}"}
41
+ api_url = f"https://api-inference.huggingface.co/models/{model_name}"
42
+
43
+ response = requests.post(
44
+ api_url,
45
+ headers=headers,
46
+ json={"inputs": conversation, "parameters": {"temperature": temperature, "max_new_tokens": max_tokens}},
47
+ timeout=60
48
+ )
49
+
50
+ response.raise_for_status()
51
+
52
+ # Parse the response
53
+ result = response.json()[0]
54
+
55
+ # Extract assistant's response
56
+ if isinstance(result, list) and len(result) > 0:
57
+ if isinstance(result[0], list) and len(result[0]) > 0:
58
+ assistant_response = result[0][0].get("generated_text", "")
59
+ else:
60
+ assistant_response = str(result[0])
61
+ else:
62
+ assistant_response = str(result)
63
+
64
+ # Add assistant response to history
65
+ history.append({"role": "user", "content": message})
66
+ history.append({"role": "assistant", "content": assistant_response})
67
+
68
+ return history
69
+
70
+ except requests.exceptions.RequestException as e:
71
+ error_msg = f"Error communicating with Hugging Face API: {str(e)}"
72
+ history.append({"role": "user", "content": message})
73
+ history.append({"role": "assistant", "content": error_msg})
74
+ return history
75
+ except Exception as e:
76
+ error_msg = f"An unexpected error occurred: {str(e)}"
77
+ history.append({"role": "user", "content": message})
78
+ history.append({"role": "assistant", "content": error_msg})
79
+ return history
80
+
81
+ # Create the Gradio interface
82
+ with gr.Blocks(
83
+ title="AI Chatbot",
84
+ description="Chat with various AI models powered by Hugging Face",
85
+ theme=gr.themes.Soft(
86
+ primary_hue="blue",
87
+ secondary_hue="indigo",
88
+ neutral_hue="slate",
89
+ font=gr.themes.GoogleFont("Inter"),
90
+ text_size="lg",
91
+ spacing_size="lg",
92
+ radius_size="md"
93
+ ).set(
94
+ button_primary_background_fill="*primary_600",
95
+ button_primary_background_fill_hover="*primary_700",
96
+ block_title_text_weight="600",
97
+ )
98
+ ) as demo:
99
+
100
+ # Header section
101
+ gr.Markdown(
102
+ """
103
+ # 🤖 AI Chatbot
104
+ Chat with powerful AI models from Hugging Face
105
+ """
106
+ )
107
+
108
+ # Model selection and settings
109
+ with gr.Accordion("⚙️ Model Settings", open=False):
110
+ with gr.Row():
111
+ with gr.Column():
112
+ model_dropdown = gr.Dropdown(
113
+ choices=[
114
+ "mistralai/Mistral-7B-Instruct-v0.2",
115
+ "meta-llama/Llama-2-7b-chat-hf",
116
+ "tiiuae/falcon-7b-instruct",
117
+ "bigscience/bloom-560m",
118
+ "google/flan-t5-large",
119
+ "gpt2-xl"
120
+ ],
121
+ value="mistralai/Mistral-7B-Instruct-v0.2",
122
+ label="Select Model",
123
+ info="Choose an AI model from Hugging Face"
124
+ )
125
+
126
+ with gr.Column():
127
+ temperature_slider = gr.Slider(
128
+ minimum=0.0,
129
+ maximum=1.0,
130
+ value=0.7,
131
+ step=0.1,
132
+ label="Temperature",
133
+ info="Lower = more focused, Higher = more creative"
134
+ )
135
+
136
+ with gr.Column():
137
+ max_tokens_slider = gr.Slider(
138
+ minimum=50,
139
+ maximum=2048,
140
+ value=512,
141
+ step=50,
142
+ label="Max Tokens",
143
+ info="Maximum length of response"
144
+ )
145
+
146
+ # API Token input
147
+ with gr.Row():
148
+ api_token = gr.Textbox(
149
+ label="Hugging Face API Token",
150
+ placeholder="Enter your HF_TOKEN environment variable or paste token here",
151
+ type="password",
152
+ info="Required for private models. Leave empty if using public models."
153
+ )
154
+
155
+ # Chat interface
156
+ with gr.Row():
157
+ with gr.Column(scale=3):
158
+ chatbot = gr.Chatbot(
159
+ label="Chat History",
160
+ height=500,
161
+ avatar_images=(
162
+ "https://api.dicebear.com/7.x/avataaars/svg?seed=AI",
163
+ "https://api.dicebear.com/7.x/avataaars/svg?seed=User"
164
+ ),
165
+ bubble_full_width=False
166
+ )
167
+
168
+ with gr.Column(scale=1):
169
+ gr.Markdown(
170
+ """
171
+ ### 📝 Tips
172
+ - Enter your API token for better performance
173
+ - Try different models for different responses
174
+ - Adjust temperature for more creative outputs
175
+ - Clear chat to start fresh
176
+ """
177
+ )
178
+
179
+ clear_button = gr.Button(
180
+ "🗑️ Clear Chat",
181
+ variant="secondary",
182
+ size="lg"
183
+ )
184
+
185
+ # User input
186
+ with gr.Row():
187
+ user_input = gr.Textbox(
188
+ label="Your Message",
189
+ placeholder="Type your message here...",
190
+ scale=4,
191
+ show_label=False
192
+ )
193
+
194
+ send_button = gr.Button(
195
+ "Send",
196
+ variant="primary",
197
+ scale=1,
198
+ size="lg"
199
+ )
200
+
201
+ # Example prompts
202
+ gr.Examples(
203
+ examples=[
204
+ ["What is machine learning?"],
205
+ ["Explain quantum computing in simple terms"],
206
+ ["Write a short poem about AI"],
207
+ ["Help me debug this code"],
208
+ ["What are the benefits of renewable energy?"],
209
+ ["Tell me a fun fact about space"],
210
+ ["How do I make a good cup of coffee?"],
211
+ ["What's the difference between Python 2 and 3?"],
212
+ ],
213
+ inputs=user_input,
214
+ label="💡 Example Prompts"
215
+ )
216
+
217
+ # Event handlers
218
+ send_button.click(
219
+ fn=chat,
220
+ inputs=[user_input, chatbot, model_dropdown, temperature_slider, max_tokens_slider],
221
+ outputs=[chatbot]
222
+ )
223
+
224
+ user_input.submit(
225
+ fn=chat,
226
+ inputs=[user_input, chatbot, model_dropdown, temperature_slider, max_tokens_slider],
227
+ outputs=[chatbot]
228
+ )
229
+
230
+ clear_button.click(
231
+ fn=lambda: [],
232
+ outputs=[chatbot]
233
+ )
234
+
235
+ # Footer
236
+ gr.Markdown(
237
+ """
238
+ ---
239
+ Built with **[anycoder](https://huggingface.co/spaces/akhaliq/anycoder)**
240
+ """
241
+ )
242
+
243
+ # Launch the application
244
+ if __name__ == "__main__":
245
+ demo.launch(
246
+ theme=gr.themes.Soft(
247
+ primary_hue="blue",
248
+ secondary_hue="indigo",
249
+ neutral_hue="slate",
250
+ font=gr.themes.GoogleFont("Inter"),
251
+ text_size="lg",
252
+ spacing_size="lg",
253
+ radius_size="md"
254
+ ).set(
255
+ button_primary_background_fill="*primary_600",
256
+ button_primary_background_fill_hover="*primary_700",
257
+ block_title_text_weight="600",
258
+ ),
259
+ css="""
260
+ .chatbot-container {
261
+ max-height: 600px !important;
262
+ }
263
+ .chatbot-message {
264
+ margin: 8px 0;
265
+ padding: 12px 16px;
266
+ border-radius: 12px;
267
+ max-width: 80%;
268
+ }
269
+ .chatbot-message.user {
270
+ background-color: #e3f2fd;
271
+ margin-left: auto;
272
+ }
273
+ .chatbot-message.assistant {
274
+ background-color: #f5f5f5;
275
+ }
276
+ """,
277
+ footer_links=[
278
+ {"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"}
279
+ ]
280
+ )
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio>=6.0
2
+ requests
3
+ Pillow