Upload folder using huggingface_hub
Browse files- .gradio/flagged/dataset1.csv +3 -0
- GradioLMstudioInterface.py +78 -68
- lmstudio_gradio.py +202 -78
.gradio/flagged/dataset1.csv
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
user_input,history,output 0,history,timestamp
|
| 2 |
+
Hi,,[],,2024-12-02 11:27:12.880943
|
| 3 |
+
,,[],,2024-12-02 12:13:03.266406
|
GradioLMstudioInterface.py
CHANGED
|
@@ -1,93 +1,103 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
|
| 4 |
-
#
|
| 5 |
BASE_URL = "http://localhost:1234/api/v0"
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
def chat_with_lmstudio(messages
|
| 9 |
-
|
| 10 |
payload = {
|
| 11 |
-
"model": model
|
| 12 |
"messages": messages,
|
| 13 |
-
"temperature":
|
| 14 |
-
"max_tokens":
|
| 15 |
"stream": False
|
| 16 |
}
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
return data["choices"][0]["message"]["content"]
|
| 22 |
-
except requests.RequestException as e:
|
| 23 |
-
return f"Error: {str(e)}"
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
def
|
| 27 |
-
|
| 28 |
payload = {
|
| 29 |
-
"model": model
|
| 30 |
"prompt": prompt,
|
| 31 |
-
"temperature":
|
| 32 |
-
"max_tokens":
|
| 33 |
"stream": False
|
| 34 |
}
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
return data["choices"][0]["text"]
|
| 40 |
-
except requests.RequestException as e:
|
| 41 |
-
return f"Error: {str(e)}"
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
def
|
| 45 |
-
|
| 46 |
payload = {
|
| 47 |
-
"model": model
|
| 48 |
"input": text
|
| 49 |
}
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
return data["data"][0]["embedding"]
|
| 55 |
-
except requests.RequestException as e:
|
| 56 |
-
return f"Error: {str(e)}"
|
| 57 |
|
| 58 |
-
# Gradio
|
| 59 |
-
def
|
| 60 |
-
|
| 61 |
-
history
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
demo.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
|
| 4 |
+
# LM Studio REST API base URL
|
| 5 |
BASE_URL = "http://localhost:1234/api/v0"
|
| 6 |
|
| 7 |
+
# Function to handle chat completions
|
| 8 |
+
def chat_with_lmstudio(messages):
|
| 9 |
+
url = f"{BASE_URL}/chat/completions"
|
| 10 |
payload = {
|
| 11 |
+
"model": "granite-3.0-2b-instruct", # Replace with the model you have loaded
|
| 12 |
"messages": messages,
|
| 13 |
+
"temperature": 0.7,
|
| 14 |
+
"max_tokens": 1024,
|
| 15 |
"stream": False
|
| 16 |
}
|
| 17 |
+
response = requests.post(url, json=payload)
|
| 18 |
+
response.raise_for_status()
|
| 19 |
+
response_data = response.json()
|
| 20 |
+
return response_data['choices'][0]['message']['content']
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
# Function to handle text completions
|
| 23 |
+
def get_text_completion(prompt):
|
| 24 |
+
url = f"{BASE_URL}/completions"
|
| 25 |
payload = {
|
| 26 |
+
"model": "granite-3.0-2b-instruct", # Replace with the model you have loaded
|
| 27 |
"prompt": prompt,
|
| 28 |
+
"temperature": 0.7,
|
| 29 |
+
"max_tokens": 100,
|
| 30 |
"stream": False
|
| 31 |
}
|
| 32 |
+
response = requests.post(url, json=payload)
|
| 33 |
+
response.raise_for_status()
|
| 34 |
+
response_data = response.json()
|
| 35 |
+
return response_data['choices'][0]['text']
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
# Function to handle text embeddings
|
| 38 |
+
def get_text_embedding(text):
|
| 39 |
+
url = f"{BASE_URL}/embeddings"
|
| 40 |
payload = {
|
| 41 |
+
"model": "text-embedding-nomic-embed-text-v1.5", # Replace with your embedding model
|
| 42 |
"input": text
|
| 43 |
}
|
| 44 |
+
response = requests.post(url, json=payload)
|
| 45 |
+
response.raise_for_status()
|
| 46 |
+
response_data = response.json()
|
| 47 |
+
return response_data['data'][0]['embedding']
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
# Gradio interface for chat
|
| 50 |
+
def gradio_chat_interface():
|
| 51 |
+
def chat_interface(user_input, history):
|
| 52 |
+
# Format history in LM Studio messages format
|
| 53 |
+
messages = []
|
| 54 |
+
for user_msg, assistant_msg in history:
|
| 55 |
+
messages.append({"role": "user", "content": user_msg})
|
| 56 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 57 |
+
messages.append({"role": "user", "content": user_input})
|
| 58 |
+
|
| 59 |
+
# Get response from LM Studio
|
| 60 |
+
response = chat_with_lmstudio(messages)
|
| 61 |
+
|
| 62 |
+
# Update history with the assistant's response
|
| 63 |
+
history.append((user_input, response))
|
| 64 |
+
return history, history
|
| 65 |
|
| 66 |
+
chat_interface = gr.ChatInterface(chat_interface, type='messages')
|
| 67 |
+
chat_interface.launch(share=True)
|
| 68 |
|
| 69 |
+
# Gradio interface for text completion
|
| 70 |
+
def gradio_text_completion():
|
| 71 |
+
gr.Interface(
|
| 72 |
+
fn=get_text_completion,
|
| 73 |
+
inputs="text",
|
| 74 |
+
outputs="text",
|
| 75 |
+
title="Text Completion with LM Studio"
|
| 76 |
+
).launch(share=True)
|
| 77 |
|
| 78 |
+
# Gradio interface for text embedding
|
| 79 |
+
def gradio_text_embedding():
|
| 80 |
+
gr.Interface(
|
| 81 |
+
fn=get_text_embedding,
|
| 82 |
+
inputs="text",
|
| 83 |
+
outputs="text",
|
| 84 |
+
title="Text Embedding with LM Studio"
|
| 85 |
+
).launch(share=True)
|
| 86 |
|
| 87 |
+
# Main menu to choose the interface
|
| 88 |
+
def main():
|
| 89 |
+
with gr.Blocks() as demo:
|
| 90 |
+
gr.Markdown("""
|
| 91 |
+
# LM Studio API Interface
|
| 92 |
+
Choose which function you want to use with LM Studio:
|
| 93 |
+
""")
|
| 94 |
+
|
| 95 |
+
with gr.Row():
|
| 96 |
+
gr.Button("Chat with Model").click(gradio_chat_interface)
|
| 97 |
+
gr.Button("Text Completion").click(gradio_text_completion)
|
| 98 |
+
gr.Button("Text Embedding").click(gradio_text_embedding)
|
| 99 |
|
| 100 |
demo.launch(share=True)
|
| 101 |
+
|
| 102 |
+
if __name__ == "__main__":
|
| 103 |
+
main()
|
lmstudio_gradio.py
CHANGED
|
@@ -1,93 +1,217 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
| 10 |
payload = {
|
| 11 |
-
"model": model
|
| 12 |
"messages": messages,
|
| 13 |
-
"temperature":
|
| 14 |
-
"max_tokens":
|
| 15 |
-
"stream":
|
| 16 |
}
|
|
|
|
|
|
|
| 17 |
try:
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
def
|
| 27 |
-
|
| 28 |
payload = {
|
| 29 |
-
"model": model
|
| 30 |
-
"prompt": prompt,
|
| 31 |
-
"temperature": temperature,
|
| 32 |
-
"max_tokens": max_tokens,
|
| 33 |
-
"stream": False
|
| 34 |
-
}
|
| 35 |
-
try:
|
| 36 |
-
response = requests.post(endpoint, json=payload)
|
| 37 |
-
response.raise_for_status()
|
| 38 |
-
data = response.json()
|
| 39 |
-
return data["choices"][0]["text"]
|
| 40 |
-
except requests.RequestException as e:
|
| 41 |
-
return f"Error: {str(e)}"
|
| 42 |
-
|
| 43 |
-
# Embeddings function
|
| 44 |
-
def text_embedding(text, model):
|
| 45 |
-
endpoint = f"{BASE_URL}/embeddings"
|
| 46 |
-
payload = {
|
| 47 |
-
"model": model,
|
| 48 |
"input": text
|
| 49 |
}
|
|
|
|
|
|
|
| 50 |
try:
|
| 51 |
-
response = requests.post(
|
| 52 |
response.raise_for_status()
|
| 53 |
data = response.json()
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
return
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
+
import logging
|
| 4 |
+
import json
|
| 5 |
+
import os
|
| 6 |
+
import numpy as np
|
| 7 |
|
| 8 |
+
# Set up logging to help troubleshoot issues
|
| 9 |
+
logging.basicConfig(level=logging.DEBUG)
|
| 10 |
|
| 11 |
+
# LM Studio REST API base URL
|
| 12 |
+
BASE_URL = "http://localhost:1234/v1"
|
| 13 |
+
|
| 14 |
+
# Function to handle chat completions with streaming support
|
| 15 |
+
def chat_with_lmstudio(messages):
|
| 16 |
+
url = f"{BASE_URL}/chat/completions"
|
| 17 |
payload = {
|
| 18 |
+
"model": "bartowski/Qwen2.5-Coder-32B-Instruct-GGUF/Qwen2.5-Coder-32B-Instruct-IQ2_M.gguf", # Replace with your chat model
|
| 19 |
"messages": messages,
|
| 20 |
+
"temperature": 0.7,
|
| 21 |
+
"max_tokens": 4096,
|
| 22 |
+
"stream": True
|
| 23 |
}
|
| 24 |
+
logging.debug(f"Sending POST request to URL: {url}")
|
| 25 |
+
logging.debug(f"Payload: {json.dumps(payload, indent=2)}")
|
| 26 |
try:
|
| 27 |
+
with requests.post(url, json=payload, stream=True) as response:
|
| 28 |
+
logging.debug(f"Response Status Code: {response.status_code}")
|
| 29 |
+
response.raise_for_status()
|
| 30 |
+
collected_response = ""
|
| 31 |
+
for chunk in response.iter_lines():
|
| 32 |
+
if chunk:
|
| 33 |
+
chunk_data = chunk.decode('utf-8').strip()
|
| 34 |
+
if chunk_data == "[DONE]":
|
| 35 |
+
logging.debug("Received [DONE] signal. Ending stream.")
|
| 36 |
+
break
|
| 37 |
+
if chunk_data.startswith("data: "):
|
| 38 |
+
chunk_data = chunk_data[6:].strip()
|
| 39 |
+
logging.debug(f"Received Chunk: {chunk_data}")
|
| 40 |
+
try:
|
| 41 |
+
response_data = json.loads(chunk_data)
|
| 42 |
+
if "choices" in response_data and len(response_data["choices"]) > 0:
|
| 43 |
+
content = response_data['choices'][0].get('delta', {}).get('content', "")
|
| 44 |
+
collected_response += content
|
| 45 |
+
yield content
|
| 46 |
+
except json.JSONDecodeError:
|
| 47 |
+
logging.error(f"Failed to decode JSON from chunk: {chunk_data}")
|
| 48 |
+
if not collected_response:
|
| 49 |
+
yield "I'm sorry, I couldn't generate a response. Could you please try again?"
|
| 50 |
+
except requests.exceptions.RequestException as e:
|
| 51 |
+
logging.error(f"Request to LM Studio failed: {e}")
|
| 52 |
+
yield "An error occurred while connecting to LM Studio. Please try again later."
|
| 53 |
|
| 54 |
+
# Function to get embeddings from LM Studio
|
| 55 |
+
def get_embeddings(text):
|
| 56 |
+
url = f"{BASE_URL}/embeddings"
|
| 57 |
payload = {
|
| 58 |
+
"model": "nomad_embed_text_v1_5_Q8_0", # Use the exact model name registered in LM Studio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
"input": text
|
| 60 |
}
|
| 61 |
+
logging.debug(f"Sending POST request to URL: {url}")
|
| 62 |
+
logging.debug(f"Payload: {json.dumps(payload, indent=2)}")
|
| 63 |
try:
|
| 64 |
+
response = requests.post(url, json=payload)
|
| 65 |
response.raise_for_status()
|
| 66 |
data = response.json()
|
| 67 |
+
embedding = data['data'][0]['embedding']
|
| 68 |
+
logging.debug(f"Received Embedding: {embedding}")
|
| 69 |
+
return embedding
|
| 70 |
+
except requests.exceptions.RequestException as e:
|
| 71 |
+
logging.error(f"Request to LM Studio for embeddings failed: {e}")
|
| 72 |
+
return None
|
| 73 |
+
|
| 74 |
+
# Function to calculate cosine similarity
|
| 75 |
+
def cosine_similarity(vec1, vec2):
|
| 76 |
+
if not vec1 or not vec2:
|
| 77 |
+
return 0
|
| 78 |
+
vec1 = np.array(vec1)
|
| 79 |
+
vec2 = np.array(vec2)
|
| 80 |
+
if np.linalg.norm(vec1) == 0 or np.linalg.norm(vec2) == 0:
|
| 81 |
+
return 0
|
| 82 |
+
return np.dot(vec1, vec2) / (np.linalg.norm(vec1) * np.linalg.norm(vec2))
|
| 83 |
+
|
| 84 |
+
# Gradio Blocks interface for chat with file upload and embeddings
|
| 85 |
+
def gradio_chat_interface():
|
| 86 |
+
with gr.Blocks() as iface:
|
| 87 |
+
gr.Markdown("# Chat with LM Studio 🚀")
|
| 88 |
+
gr.Markdown("A chat interface powered by LM Studio. You can send text messages or upload files (e.g., `.txt`) to include in the conversation.")
|
| 89 |
+
|
| 90 |
+
chatbot = gr.Chatbot(type='messages') # Specify 'messages' type to avoid deprecated tuple format
|
| 91 |
+
state = gr.State([]) # To store conversation history as list of dicts
|
| 92 |
+
embeddings_state = gr.State([]) # To store embeddings
|
| 93 |
+
|
| 94 |
+
with gr.Row():
|
| 95 |
+
with gr.Column(scale=4):
|
| 96 |
+
user_input = gr.Textbox(
|
| 97 |
+
label="Type your message here",
|
| 98 |
+
placeholder="Enter text and press enter",
|
| 99 |
+
lines=1
|
| 100 |
+
)
|
| 101 |
+
with gr.Column(scale=1):
|
| 102 |
+
file_input = gr.File(
|
| 103 |
+
label="Upload a file",
|
| 104 |
+
file_types=[".txt"], # Restrict to text files; modify as needed
|
| 105 |
+
type="binary" # Corrected from 'file' to 'binary'
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
send_button = gr.Button("Send")
|
| 109 |
+
|
| 110 |
+
# Function to handle chat interactions
|
| 111 |
+
def chat_interface(user_message, uploaded_file, history, embeddings):
|
| 112 |
+
# Initialize history and embeddings if None
|
| 113 |
+
if history is None:
|
| 114 |
+
history = []
|
| 115 |
+
if embeddings is None:
|
| 116 |
+
embeddings = []
|
| 117 |
+
|
| 118 |
+
# Process uploaded file if present
|
| 119 |
+
if uploaded_file is not None:
|
| 120 |
+
try:
|
| 121 |
+
# Read the uploaded file's content
|
| 122 |
+
file_content = uploaded_file.read().decode('utf-8')
|
| 123 |
+
user_message += f"\n\n[File Content]:\n{file_content}"
|
| 124 |
+
logging.debug(f"Processed uploaded file: {uploaded_file.name}")
|
| 125 |
+
|
| 126 |
+
# Generate embedding for the file content
|
| 127 |
+
file_embedding = get_embeddings(file_content)
|
| 128 |
+
if file_embedding:
|
| 129 |
+
embeddings.append((file_content, file_embedding))
|
| 130 |
+
logging.debug(f"Stored embedding for uploaded file: {uploaded_file.name}")
|
| 131 |
+
except Exception as e:
|
| 132 |
+
logging.error(f"Error reading uploaded file: {e}")
|
| 133 |
+
user_message += "\n\n[Error reading the uploaded file.]"
|
| 134 |
+
|
| 135 |
+
# Generate embedding for the user message
|
| 136 |
+
user_embedding = get_embeddings(user_message)
|
| 137 |
+
if user_embedding:
|
| 138 |
+
embeddings.append((user_message, user_embedding))
|
| 139 |
+
logging.debug("Stored embedding for user message.")
|
| 140 |
+
|
| 141 |
+
# Retrieve relevant context based on embeddings (optional)
|
| 142 |
+
# For demonstration, we'll retrieve top 2 similar past messages
|
| 143 |
+
context_messages = []
|
| 144 |
+
if embeddings:
|
| 145 |
+
similarities = []
|
| 146 |
+
for idx, (text, embed) in enumerate(embeddings[:-1]): # Exclude the current user message
|
| 147 |
+
sim = cosine_similarity(user_embedding, embed)
|
| 148 |
+
similarities.append((sim, idx))
|
| 149 |
+
# Sort by similarity
|
| 150 |
+
similarities.sort(reverse=True, key=lambda x: x[0])
|
| 151 |
+
top_n = 2
|
| 152 |
+
top_indices = [idx for (_, idx) in similarities[:top_n]]
|
| 153 |
+
for idx in top_indices:
|
| 154 |
+
context_messages.append(history[idx]['content']) # Append user messages as context
|
| 155 |
+
|
| 156 |
+
# Append user message to history
|
| 157 |
+
history.append({"role": "user", "content": user_message})
|
| 158 |
+
logging.debug(f"Updated History: {history}")
|
| 159 |
+
|
| 160 |
+
# Format history with additional context
|
| 161 |
+
messages = []
|
| 162 |
+
if context_messages:
|
| 163 |
+
messages.append({"role": "system", "content": "You have the following context:"})
|
| 164 |
+
for ctx in context_messages:
|
| 165 |
+
messages.append({"role": "user", "content": ctx})
|
| 166 |
+
messages.append({"role": "system", "content": "Use this context to assist the user."})
|
| 167 |
+
|
| 168 |
+
# Append all messages from history
|
| 169 |
+
messages.extend(history)
|
| 170 |
+
|
| 171 |
+
# Get response from LM Studio
|
| 172 |
+
response_stream = chat_with_lmstudio(messages)
|
| 173 |
+
response = ""
|
| 174 |
+
|
| 175 |
+
# To handle streaming, we'll initialize the assistant message and update it incrementally
|
| 176 |
+
assistant_message = {"role": "assistant", "content": ""}
|
| 177 |
+
history.append(assistant_message)
|
| 178 |
+
logging.debug(f"Appended empty assistant message: {assistant_message}")
|
| 179 |
+
|
| 180 |
+
for chunk in response_stream:
|
| 181 |
+
response += chunk
|
| 182 |
+
# Update the assistant message content
|
| 183 |
+
assistant_message['content'] = response
|
| 184 |
+
logging.debug(f"Updated assistant message: {assistant_message}")
|
| 185 |
+
# Yield the updated history and embeddings
|
| 186 |
+
yield history, embeddings
|
| 187 |
+
|
| 188 |
+
# Finalize the history with the complete response
|
| 189 |
+
assistant_message['content'] = response
|
| 190 |
+
logging.debug(f"Final assistant message: {assistant_message}")
|
| 191 |
+
yield history, embeddings
|
| 192 |
+
|
| 193 |
+
# Connect the send button to the chat function
|
| 194 |
+
send_button.click(
|
| 195 |
+
fn=chat_interface,
|
| 196 |
+
inputs=[user_input, file_input, state, embeddings_state],
|
| 197 |
+
outputs=[chatbot, embeddings_state],
|
| 198 |
+
queue=True # Enable queuing for handling multiple requests
|
| 199 |
+
)
|
| 200 |
+
|
| 201 |
+
# Also allow pressing Enter in the textbox to send the message
|
| 202 |
+
user_input.submit(
|
| 203 |
+
fn=chat_interface,
|
| 204 |
+
inputs=[user_input, file_input, state, embeddings_state],
|
| 205 |
+
outputs=[chatbot, embeddings_state],
|
| 206 |
+
queue=True
|
| 207 |
+
)
|
| 208 |
+
|
| 209 |
+
# Add debug statements to determine file pattern issues
|
| 210 |
+
logging.debug(f"Current working directory: {os.getcwd()}")
|
| 211 |
+
logging.debug(f"Files in current directory: {os.listdir(os.getcwd())}")
|
| 212 |
+
|
| 213 |
+
iface.launch(share=True)
|
| 214 |
+
|
| 215 |
+
# Main function to launch the chat interface
|
| 216 |
+
if __name__ == "__main__":
|
| 217 |
+
gradio_chat_interface()
|