Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,6 @@
|
|
1 |
import os
|
|
|
|
|
2 |
import gradio as gr
|
3 |
from huggingface_hub import InferenceClient
|
4 |
|
@@ -11,10 +13,14 @@ class XylariaChat:
|
|
11 |
|
12 |
# Initialize the inference client
|
13 |
self.client = InferenceClient(
|
14 |
-
model=
|
15 |
api_key=self.hf_token
|
16 |
)
|
17 |
|
|
|
|
|
|
|
|
|
18 |
# Initialize conversation history and persistent memory
|
19 |
self.conversation_history = []
|
20 |
self.persistent_memory = {}
|
@@ -22,6 +28,7 @@ class XylariaChat:
|
|
22 |
# System prompt with more detailed instructions
|
23 |
self.system_prompt = """You are a helpful and harmless assistant. You are Xylaria developed by Sk Md Saad Amin(india, 12 year old). You should think step-by-step.
|
24 |
"""
|
|
|
25 |
def store_information(self, key, value):
|
26 |
"""Store important information in persistent memory"""
|
27 |
self.persistent_memory[key] = value
|
@@ -51,12 +58,53 @@ class XylariaChat:
|
|
51 |
|
52 |
return None # To clear the chatbot interface
|
53 |
|
54 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
# Prepare messages with conversation context and persistent memory
|
56 |
messages = [
|
57 |
{"role": "system", "content": self.system_prompt},
|
58 |
*self.conversation_history,
|
59 |
-
{"role": "user", "content": user_input}
|
60 |
]
|
61 |
|
62 |
# Add persistent memory context if available
|
@@ -66,6 +114,14 @@ class XylariaChat:
|
|
66 |
)
|
67 |
messages.insert(1, {"role": "system", "content": memory_context})
|
68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
# Generate response with streaming
|
70 |
try:
|
71 |
stream = self.client.chat.completions.create(
|
@@ -82,13 +138,13 @@ class XylariaChat:
|
|
82 |
return f"Error generating response: {str(e)}"
|
83 |
|
84 |
def create_interface(self):
|
85 |
-
def streaming_response(message, chat_history):
|
86 |
# Clear input textbox
|
87 |
-
response_stream = self.get_response(message)
|
88 |
|
89 |
# If it's an error, return immediately
|
90 |
if isinstance(response_stream, str):
|
91 |
-
return "", chat_history + [[message, response_stream]]
|
92 |
|
93 |
# Prepare for streaming response
|
94 |
full_response = ""
|
@@ -102,7 +158,7 @@ class XylariaChat:
|
|
102 |
|
103 |
# Update the last message in chat history with partial response
|
104 |
updated_history[-1][1] = full_response
|
105 |
-
yield "", updated_history
|
106 |
|
107 |
# Update conversation history
|
108 |
self.conversation_history.append(
|
@@ -144,30 +200,37 @@ class XylariaChat:
|
|
144 |
show_copy_button=True
|
145 |
)
|
146 |
|
147 |
-
# Input row with improved layout
|
148 |
with gr.Row():
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
155 |
btn = gr.Button("Send", scale=1)
|
156 |
|
157 |
# Clear history and memory buttons
|
158 |
-
|
159 |
-
|
|
|
160 |
|
161 |
-
# Submit functionality with streaming
|
162 |
btn.click(
|
163 |
fn=streaming_response,
|
164 |
-
inputs=[txt, chatbot],
|
165 |
-
outputs=[txt, chatbot]
|
166 |
)
|
167 |
txt.submit(
|
168 |
fn=streaming_response,
|
169 |
-
inputs=[txt, chatbot],
|
170 |
-
outputs=[txt, chatbot]
|
171 |
)
|
172 |
|
173 |
# Clear conversation history
|
|
|
1 |
import os
|
2 |
+
import base64
|
3 |
+
import requests
|
4 |
import gradio as gr
|
5 |
from huggingface_hub import InferenceClient
|
6 |
|
|
|
13 |
|
14 |
# Initialize the inference client
|
15 |
self.client = InferenceClient(
|
16 |
+
model=os.getenv("MODEL_NAME"),
|
17 |
api_key=self.hf_token
|
18 |
)
|
19 |
|
20 |
+
# Image captioning API setup
|
21 |
+
self.image_api_url = "https://api-inference.huggingface.co/models/microsoft/git-large-coco"
|
22 |
+
self.image_api_headers = {"Authorization": f"Bearer {self.hf_token}"}
|
23 |
+
|
24 |
# Initialize conversation history and persistent memory
|
25 |
self.conversation_history = []
|
26 |
self.persistent_memory = {}
|
|
|
28 |
# System prompt with more detailed instructions
|
29 |
self.system_prompt = """You are a helpful and harmless assistant. You are Xylaria developed by Sk Md Saad Amin(india, 12 year old). You should think step-by-step.
|
30 |
"""
|
31 |
+
|
32 |
def store_information(self, key, value):
|
33 |
"""Store important information in persistent memory"""
|
34 |
self.persistent_memory[key] = value
|
|
|
58 |
|
59 |
return None # To clear the chatbot interface
|
60 |
|
61 |
+
def caption_image(self, image):
|
62 |
+
"""
|
63 |
+
Caption an uploaded image using Hugging Face API
|
64 |
+
|
65 |
+
Args:
|
66 |
+
image (str): Base64 encoded image or file path
|
67 |
+
|
68 |
+
Returns:
|
69 |
+
str: Image caption or error message
|
70 |
+
"""
|
71 |
+
try:
|
72 |
+
# If image is a file path, read and encode
|
73 |
+
if isinstance(image, str) and os.path.isfile(image):
|
74 |
+
with open(image, "rb") as f:
|
75 |
+
data = f.read()
|
76 |
+
# If image is already base64 encoded
|
77 |
+
elif isinstance(image, str):
|
78 |
+
# Remove data URI prefix if present
|
79 |
+
if image.startswith('data:image'):
|
80 |
+
image = image.split(',')[1]
|
81 |
+
data = base64.b64decode(image)
|
82 |
+
# If image is a file-like object
|
83 |
+
else:
|
84 |
+
data = image.read()
|
85 |
+
|
86 |
+
# Send request to Hugging Face API
|
87 |
+
response = requests.post(
|
88 |
+
self.image_api_url,
|
89 |
+
headers=self.image_api_headers,
|
90 |
+
data=data
|
91 |
+
)
|
92 |
+
|
93 |
+
# Check response
|
94 |
+
if response.status_code == 200:
|
95 |
+
caption = response.json()[0].get('generated_text', 'No caption generated')
|
96 |
+
return caption
|
97 |
+
else:
|
98 |
+
return f"Error captioning image: {response.text}"
|
99 |
+
|
100 |
+
except Exception as e:
|
101 |
+
return f"Error processing image: {str(e)}"
|
102 |
+
|
103 |
+
def get_response(self, user_input, image=None):
|
104 |
# Prepare messages with conversation context and persistent memory
|
105 |
messages = [
|
106 |
{"role": "system", "content": self.system_prompt},
|
107 |
*self.conversation_history,
|
|
|
108 |
]
|
109 |
|
110 |
# Add persistent memory context if available
|
|
|
114 |
)
|
115 |
messages.insert(1, {"role": "system", "content": memory_context})
|
116 |
|
117 |
+
# Process image if uploaded
|
118 |
+
if image:
|
119 |
+
image_caption = self.caption_image(image)
|
120 |
+
user_input = f"Image description: {image_caption}\n\nUser's message: {user_input}"
|
121 |
+
|
122 |
+
# Add user input
|
123 |
+
messages.append({"role": "user", "content": user_input})
|
124 |
+
|
125 |
# Generate response with streaming
|
126 |
try:
|
127 |
stream = self.client.chat.completions.create(
|
|
|
138 |
return f"Error generating response: {str(e)}"
|
139 |
|
140 |
def create_interface(self):
|
141 |
+
def streaming_response(message, chat_history, image):
|
142 |
# Clear input textbox
|
143 |
+
response_stream = self.get_response(message, image)
|
144 |
|
145 |
# If it's an error, return immediately
|
146 |
if isinstance(response_stream, str):
|
147 |
+
return "", chat_history + [[message, response_stream]], None
|
148 |
|
149 |
# Prepare for streaming response
|
150 |
full_response = ""
|
|
|
158 |
|
159 |
# Update the last message in chat history with partial response
|
160 |
updated_history[-1][1] = full_response
|
161 |
+
yield "", updated_history, None
|
162 |
|
163 |
# Update conversation history
|
164 |
self.conversation_history.append(
|
|
|
200 |
show_copy_button=True
|
201 |
)
|
202 |
|
203 |
+
# Input row with improved layout and image upload
|
204 |
with gr.Row():
|
205 |
+
with gr.Column(scale=4):
|
206 |
+
txt = gr.Textbox(
|
207 |
+
show_label=False,
|
208 |
+
placeholder="Type your message...",
|
209 |
+
container=False
|
210 |
+
)
|
211 |
+
img = gr.Image(
|
212 |
+
sources=["upload", "camera"],
|
213 |
+
type="filepath",
|
214 |
+
label="Upload or Capture Image"
|
215 |
+
)
|
216 |
+
|
217 |
btn = gr.Button("Send", scale=1)
|
218 |
|
219 |
# Clear history and memory buttons
|
220 |
+
with gr.Row():
|
221 |
+
clear = gr.Button("Clear Conversation")
|
222 |
+
clear_memory = gr.Button("Clear Memory")
|
223 |
|
224 |
+
# Submit functionality with streaming and image support
|
225 |
btn.click(
|
226 |
fn=streaming_response,
|
227 |
+
inputs=[txt, chatbot, img],
|
228 |
+
outputs=[txt, chatbot, img]
|
229 |
)
|
230 |
txt.submit(
|
231 |
fn=streaming_response,
|
232 |
+
inputs=[txt, chatbot, img],
|
233 |
+
outputs=[txt, chatbot, img]
|
234 |
)
|
235 |
|
236 |
# Clear conversation history
|