darkc0de commited on
Commit
2aef932
·
verified ·
1 Parent(s): b3c2d30

Upload 2 files

Browse files
Files changed (2) hide show
  1. app (3).py +148 -0
  2. requirements (1).txt +2 -0
app (3).py ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 = 99999 # 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://c63d-69-58-93-129.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
+ # Create a Gradio Blocks layout for more control over the interface
106
+ # theme=gr.themes.Soft() applies a soft visual theme
107
+ # Add the kofi_script to the head of the HTML page
108
+ with gr.Blocks(theme=gr.themes.Soft(), head=kofi_script) as demo:
109
+ # Display an image at the top of the chatbot interface
110
+ gr.Image(
111
+ value=header_image_path, # Source of the image
112
+ label="Chatbot Header", # Alt text or label (not shown due to show_label=False)
113
+ show_label=False, # Hide the label text
114
+ interactive=False, # Make the image non-interactive
115
+ height=100, # Set the height of the image
116
+ elem_id="chatbot-logo" # Assign an HTML ID for potential CSS styling
117
+ )
118
+
119
+ # Create the chat interface component
120
+ gr.ChatInterface(
121
+ fn=respond, # The function to call when a message is sent
122
+ chatbot=gr.Chatbot( # Configure the chatbot display area
123
+ height=500 # Set the height of the chat history display
124
+ ),
125
+ # Additional parameters for ChatInterface can be added here, e.g.:
126
+ # title="Xortron7 Chat",
127
+ # description="Chat with Xortron7, your AI assistant.",
128
+ # examples=[["Hello!", None], ["What is Gradio?", None]],
129
+ # retry_btn=None, # Removes the retry button
130
+ # undo_btn="Delete Previous", # Customizes the undo button
131
+ # clear_btn="Clear Chat", # Customizes the clear button
132
+ )
133
+
134
+ # --- Application Entry Point ---
135
+ if __name__ == "__main__":
136
+ # Launch the Gradio web server
137
+ # show_api=False disables the API documentation page
138
+ # share=False prevents creating a public Gradio link (for local development)
139
+ try:
140
+ demo.launch(show_api=False, share=False)
141
+ except NameError as ne:
142
+ # This might happen if 'client' was not defined due to an error during initialization
143
+ print(f"Gradio demo could not be launched. 'client' might not have been initialized: {ne}")
144
+ except RuntimeError as re:
145
+ # This catches the RuntimeError raised if client initialization failed explicitly
146
+ print(f"Gradio demo could not be launched due to an error during client initialization: {re}")
147
+ except Exception as e:
148
+ print(f"An unexpected error occurred when trying to launch Gradio demo: {e}")
requirements (1).txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ huggingface_hub==0.25.2
2
+ openai