Spaces:
Running
Running
Upload app (4).py
Browse files- app (4).py +160 -0
app (4).py
ADDED
@@ -0,0 +1,160 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Import the Gradio library for creating the web interface
|
2 |
+
import gradio as gr
|
3 |
+
# Import the InferenceClient from huggingface_hub to interact with the language model
|
4 |
+
from huggingface_hub import InferenceClient
|
5 |
+
|
6 |
+
# --- Configuration Constants ---
|
7 |
+
# Define the maximum number of tokens the model should generate in a single response
|
8 |
+
FIXED_MAX_TOKENS = 1000 # Note: This is a very high value, typical values are much lower (e.g., 512, 1024, 2048, 4096 for many models)
|
9 |
+
|
10 |
+
|
11 |
+
# --- Initialize the InferenceClient ---
|
12 |
+
# For custom OpenAI-compatible APIs, initialize the InferenceClient with the base URL.
|
13 |
+
# The specific model will be specified in the API call (e.g., chat_completion).
|
14 |
+
API_BASE_URL = "https://vulture-awake-probably.ngrok-free.app/v1/chat/completions" # Base URL for the custom API
|
15 |
+
|
16 |
+
try:
|
17 |
+
# Initialize the client with the base URL of your API.
|
18 |
+
# If your API requires an authentication token, you might need to pass it here,
|
19 |
+
# e.g., client = InferenceClient(base_url=API_BASE_URL, token="YOUR_API_TOKEN")
|
20 |
+
# or ensure it's set as an environment variable if the client/API supports that.
|
21 |
+
client = InferenceClient(base_url=API_BASE_URL)
|
22 |
+
print(f"InferenceClient initialized with base_url: {API_BASE_URL}")
|
23 |
+
except Exception as e:
|
24 |
+
print(f"Error initializing InferenceClient with base_url '{API_BASE_URL}': {e}")
|
25 |
+
# Handle the error appropriately, e.g., by exiting or using a fallback
|
26 |
+
raise RuntimeError(
|
27 |
+
"Could not initialize InferenceClient. "
|
28 |
+
f"Please check the API base URL ('{API_BASE_URL}') and ensure the server is accessible. "
|
29 |
+
f"Error: {e}"
|
30 |
+
)
|
31 |
+
|
32 |
+
|
33 |
+
# --- Core Chatbot Logic ---
|
34 |
+
def respond(message, history):
|
35 |
+
"""
|
36 |
+
This function processes the user's message and the chat history to generate a response
|
37 |
+
from the language model using the custom API.
|
38 |
+
|
39 |
+
Args:
|
40 |
+
message (str): The latest message from the user.
|
41 |
+
history (list of lists): A list where each inner list contains a pair of
|
42 |
+
[user_message, ai_message].
|
43 |
+
|
44 |
+
Yields:
|
45 |
+
str: The generated response token by token (for streaming).
|
46 |
+
"""
|
47 |
+
# Initialize the messages list
|
48 |
+
messages = []
|
49 |
+
|
50 |
+
# Append past interactions from the history to the messages list
|
51 |
+
# This provides context to the language model
|
52 |
+
for user_message, ai_message in history:
|
53 |
+
if user_message: # Ensure there's a user message
|
54 |
+
messages.append({"role": "user", "content": user_message})
|
55 |
+
if ai_message: # Ensure there's an AI message
|
56 |
+
messages.append({"role": "assistant", "content": ai_message})
|
57 |
+
|
58 |
+
# Append the current user's message to the messages list
|
59 |
+
messages.append({"role": "user", "content": message})
|
60 |
+
|
61 |
+
# Initialize an empty string to accumulate the response
|
62 |
+
response_text = ""
|
63 |
+
|
64 |
+
try:
|
65 |
+
# Make a streaming call to the language model's chat completions endpoint.
|
66 |
+
# The `model` parameter specifies which model to use at the endpoint.
|
67 |
+
stream = client.chat_completion(
|
68 |
+
messages=messages, # The conversation history and current message
|
69 |
+
max_tokens=FIXED_MAX_TOKENS, # Maximum tokens for the response
|
70 |
+
stream=True, # Enable streaming for token-by-token output
|
71 |
+
)
|
72 |
+
|
73 |
+
for chunk in stream:
|
74 |
+
# Check if the chunk contains content and the content is not None
|
75 |
+
# The exact structure of the chunk can vary based on the model/endpoint
|
76 |
+
if chunk.choices and chunk.choices[0].delta and chunk.choices[0].delta.content is not None:
|
77 |
+
token = chunk.choices[0].delta.content # Extract the token from the chunk
|
78 |
+
response_text += token # Append the token to the response string
|
79 |
+
yield response_text # Yield the accumulated response so far (for streaming UI update)
|
80 |
+
|
81 |
+
except Exception as e:
|
82 |
+
# If any error occurs during the API call, yield an error message
|
83 |
+
error_message = f"An error occurred during model inference: {e}"
|
84 |
+
print(error_message) # Also print to console for debugging
|
85 |
+
yield error_message
|
86 |
+
|
87 |
+
# --- Gradio Interface Definition ---
|
88 |
+
|
89 |
+
# URL for the header image
|
90 |
+
header_image_path = "https://cdn-uploads.huggingface.co/production/uploads/6540a02d1389943fef4d2640/j61iZTDaK9g0UW3aWGwWi.gif"
|
91 |
+
|
92 |
+
# Ko-fi widget script
|
93 |
+
kofi_script = """
|
94 |
+
<script src='https://storage.ko-fi.com/cdn/scripts/overlay-widget.js'></script>
|
95 |
+
<script>
|
96 |
+
kofiWidgetOverlay.draw('sonnydesorbo', {
|
97 |
+
'type': 'floating-chat',
|
98 |
+
'floating-chat.donateButton.text': 'Support me',
|
99 |
+
'floating-chat.donateButton.background-color': '#00b9fe',
|
100 |
+
'floating-chat.donateButton.text-color': '#fff'
|
101 |
+
});
|
102 |
+
</script>
|
103 |
+
"""
|
104 |
+
|
105 |
+
# Ko-fi button HTML
|
106 |
+
kofi_button_html = """
|
107 |
+
<div style="text-align: center; padding: 20px;">
|
108 |
+
<a href='https://ko-fi.com/Z8Z51E5TIG' target='_blank'>
|
109 |
+
<img height='36' style='border:0px;height:36px;' src='https://storage.ko-fi.com/cdn/kofi5.png?v=6' border='0' alt='Buy Me a Coffee at ko-fi.com' />
|
110 |
+
</a>
|
111 |
+
</div>
|
112 |
+
"""
|
113 |
+
|
114 |
+
# Create a Gradio Blocks layout for more control over the interface
|
115 |
+
# theme=gr.themes.Soft() applies a soft visual theme
|
116 |
+
# Add the kofi_script to the head of the HTML page
|
117 |
+
with gr.Blocks(theme=gr.themes.Soft(), head=kofi_script) as demo:
|
118 |
+
# Display an image at the top of the chatbot interface
|
119 |
+
gr.Image(
|
120 |
+
value=header_image_path, # Source of the image
|
121 |
+
label="Chatbot Header", # Alt text or label (not shown due to show_label=False)
|
122 |
+
show_label=False, # Hide the label text
|
123 |
+
interactive=False, # Make the image non-interactive
|
124 |
+
height=150, # Set the height of the image
|
125 |
+
elem_id="chatbot-logo" # Assign an HTML ID for potential CSS styling
|
126 |
+
)
|
127 |
+
|
128 |
+
# Create the chat interface component
|
129 |
+
gr.ChatInterface(
|
130 |
+
fn=respond, # The function to call when a message is sent
|
131 |
+
chatbot=gr.Chatbot( # Configure the chatbot display area
|
132 |
+
height=650 # Set the height of the chat history display
|
133 |
+
),
|
134 |
+
# Additional parameters for ChatInterface can be added here, e.g.:
|
135 |
+
# title="Xortron7 Chat",
|
136 |
+
# description="Chat with Xortron7, your AI assistant.",
|
137 |
+
# examples=[["Hello!", None], ["What is Gradio?", None]],
|
138 |
+
# retry_btn=None, # Removes the retry button
|
139 |
+
# undo_btn="Delete Previous", # Customizes the undo button
|
140 |
+
# clear_btn="Clear Chat", # Customizes the clear button
|
141 |
+
)
|
142 |
+
|
143 |
+
# Add the Ko-fi button at the bottom
|
144 |
+
gr.HTML(kofi_button_html) #
|
145 |
+
|
146 |
+
# --- Application Entry Point ---
|
147 |
+
if __name__ == "__main__":
|
148 |
+
# Launch the Gradio web server
|
149 |
+
# show_api=False disables the API documentation page
|
150 |
+
# share=False prevents creating a public Gradio link (for local development)
|
151 |
+
try:
|
152 |
+
demo.launch(show_api=False, share=False)
|
153 |
+
except NameError as ne:
|
154 |
+
# This might happen if 'client' was not defined due to an error during initialization
|
155 |
+
print(f"Gradio demo could not be launched. 'client' might not have been initialized: {ne}")
|
156 |
+
except RuntimeError as re:
|
157 |
+
# This catches the RuntimeError raised if client initialization failed explicitly
|
158 |
+
print(f"Gradio demo could not be launched due to an error during client initialization: {re}")
|
159 |
+
except Exception as e:
|
160 |
+
print(f"An unexpected error occurred when trying to launch Gradio demo: {e}")
|