Update app.py
Browse files
app.py
CHANGED
@@ -66,7 +66,7 @@ class ConversationMemory:
|
|
66 |
cursor = self.conn.execute("SELECT * FROM conversation_chunks ORDER BY timestamp DESC LIMIT ?", (limit,))
|
67 |
return [{"chunk_id": row[0], "text": row[1], "role": row[2], "timestamp": row[3], "intent": row[4], "token_count": row[5]} for row in cursor]
|
68 |
|
69 |
-
# TextEditor class (
|
70 |
class TextEditor:
|
71 |
def __init__(self, memory):
|
72 |
self.memory = memory
|
@@ -104,7 +104,7 @@ class TextEditor:
|
|
104 |
chunk = self.memory.get_chunk(chunk_id)
|
105 |
if chunk:
|
106 |
chunk['text'] = chunk['text'] + suffix
|
107 |
-
self.memory
|
108 |
return chunk['text']
|
109 |
|
110 |
def diff(self, chunk_id, original_text):
|
@@ -115,14 +115,14 @@ class TextEditor:
|
|
115 |
return '\n'.join(diff)
|
116 |
return ""
|
117 |
|
118 |
-
# OpenAIApi class (
|
119 |
class OpenAIApi:
|
120 |
def __init__(self, preprompt="", endpoint="https://T-App-GPT4o.openai.azure.com/openai/v1/", model="gpt-4o", api_key=None):
|
121 |
self.client = AzureOpenAI(
|
122 |
azure_endpoint=endpoint,
|
123 |
api_key=api_key or os.getenv("AZURE_OPENAI_API_KEY"),
|
124 |
api_version="2025-01-01-preview",
|
125 |
-
http_client_kwargs={"proxies": None}
|
126 |
)
|
127 |
self.model = model
|
128 |
self.preprompt = preprompt
|
@@ -159,7 +159,7 @@ class OpenAIApi:
|
|
159 |
},
|
160 |
{
|
161 |
"type": "function",
|
162 |
-
"name": "
|
163 |
"description": "Paste clipboard content into a conversation chunk.",
|
164 |
"parameters": {
|
165 |
"type": "object",
|
@@ -261,7 +261,7 @@ class OpenAIApi:
|
|
261 |
self.memory.add_chunk(error_msg, "system")
|
262 |
return {"error": error_msg}
|
263 |
|
264 |
-
#
|
265 |
async def chat_submit(user_input, chat_history, preprompt):
|
266 |
api = OpenAIApi(preprompt=preprompt, api_key=os.getenv("AZURE_OPENAI_API_KEY"))
|
267 |
response = await api.fetch_response(user_input)
|
@@ -280,7 +280,7 @@ def select_chunk(evt: gr.SelectData):
|
|
280 |
return evt.value["chunk_id"], evt.value["text"]
|
281 |
|
282 |
async def edit_cut(chunk_id, start, end):
|
283 |
-
api = OpenAIApi(api_key=os.getenv("
|
284 |
result = api.editor.cut(chunk_id, int(start), int(end))
|
285 |
return result, api.editor.diff(chunk_id, result)
|
286 |
|
@@ -309,7 +309,7 @@ def create_ui():
|
|
309 |
gr.Markdown("# Azure OpenAI Chat with Text Editing")
|
310 |
|
311 |
with gr.Tab("Chat"):
|
312 |
-
chatbot = gr.Chatbot(label="Conversation", type="messages")
|
313 |
user_input = gr.Textbox(label="Your Message", placeholder="Type your message or editing command...")
|
314 |
preprompt = gr.Textbox(label="System Prompt", value="You are a helpful assistant with text editing capabilities.")
|
315 |
submit_btn = gr.Button("Send")
|
|
|
66 |
cursor = self.conn.execute("SELECT * FROM conversation_chunks ORDER BY timestamp DESC LIMIT ?", (limit,))
|
67 |
return [{"chunk_id": row[0], "text": row[1], "role": row[2], "timestamp": row[3], "intent": row[4], "token_count": row[5]} for row in cursor]
|
68 |
|
69 |
+
# TextEditor class (fixed typo in add_suffix)
|
70 |
class TextEditor:
|
71 |
def __init__(self, memory):
|
72 |
self.memory = memory
|
|
|
104 |
chunk = self.memory.get_chunk(chunk_id)
|
105 |
if chunk:
|
106 |
chunk['text'] = chunk['text'] + suffix
|
107 |
+
self.memory.update_chunk(chunk_id, chunk['text']) # Fixed typo
|
108 |
return chunk['text']
|
109 |
|
110 |
def diff(self, chunk_id, original_text):
|
|
|
115 |
return '\n'.join(diff)
|
116 |
return ""
|
117 |
|
118 |
+
# OpenAIApi class (unchanged, includes proxies fix)
|
119 |
class OpenAIApi:
|
120 |
def __init__(self, preprompt="", endpoint="https://T-App-GPT4o.openai.azure.com/openai/v1/", model="gpt-4o", api_key=None):
|
121 |
self.client = AzureOpenAI(
|
122 |
azure_endpoint=endpoint,
|
123 |
api_key=api_key or os.getenv("AZURE_OPENAI_API_KEY"),
|
124 |
api_version="2025-01-01-preview",
|
125 |
+
http_client_kwargs={"proxies": None}
|
126 |
)
|
127 |
self.model = model
|
128 |
self.preprompt = preprompt
|
|
|
159 |
},
|
160 |
{
|
161 |
"type": "function",
|
162 |
+
"name": "paste_text",
|
163 |
"description": "Paste clipboard content into a conversation chunk.",
|
164 |
"parameters": {
|
165 |
"type": "object",
|
|
|
261 |
self.memory.add_chunk(error_msg, "system")
|
262 |
return {"error": error_msg}
|
263 |
|
264 |
+
# Gradio UI (unchanged, uses messages format)
|
265 |
async def chat_submit(user_input, chat_history, preprompt):
|
266 |
api = OpenAIApi(preprompt=preprompt, api_key=os.getenv("AZURE_OPENAI_API_KEY"))
|
267 |
response = await api.fetch_response(user_input)
|
|
|
280 |
return evt.value["chunk_id"], evt.value["text"]
|
281 |
|
282 |
async def edit_cut(chunk_id, start, end):
|
283 |
+
api = OpenAIApi(api_key=os.getenv("AZURE_OPENAI_API_KEY"))
|
284 |
result = api.editor.cut(chunk_id, int(start), int(end))
|
285 |
return result, api.editor.diff(chunk_id, result)
|
286 |
|
|
|
309 |
gr.Markdown("# Azure OpenAI Chat with Text Editing")
|
310 |
|
311 |
with gr.Tab("Chat"):
|
312 |
+
chatbot = gr.Chatbot(label="Conversation", type="messages")
|
313 |
user_input = gr.Textbox(label="Your Message", placeholder="Type your message or editing command...")
|
314 |
preprompt = gr.Textbox(label="System Prompt", value="You are a helpful assistant with text editing capabilities.")
|
315 |
submit_btn = gr.Button("Send")
|