Upload folder using huggingface_hub
Browse files- GradioLMstudioInterface.py +69 -79
GradioLMstudioInterface.py
CHANGED
|
@@ -1,103 +1,93 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
|
| 4 |
-
# LM Studio REST API
|
| 5 |
BASE_URL = "http://localhost:1234/api/v0"
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
def chat_with_lmstudio(messages):
|
| 9 |
-
|
| 10 |
payload = {
|
| 11 |
-
"model":
|
| 12 |
"messages": messages,
|
| 13 |
-
"temperature":
|
| 14 |
-
"max_tokens":
|
| 15 |
"stream": False
|
| 16 |
}
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
def
|
| 24 |
-
|
| 25 |
payload = {
|
| 26 |
-
"model":
|
| 27 |
"prompt": prompt,
|
| 28 |
-
"temperature":
|
| 29 |
-
"max_tokens":
|
| 30 |
"stream": False
|
| 31 |
}
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
def
|
| 39 |
-
|
| 40 |
payload = {
|
| 41 |
-
"model":
|
| 42 |
"input": text
|
| 43 |
}
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
# Gradio
|
| 50 |
-
def
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 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 |
-
|
| 67 |
-
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
).launch()
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
outputs=
|
| 84 |
-
title="Text Embedding with LM Studio"
|
| 85 |
-
).launch()
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
gr.
|
| 91 |
-
|
| 92 |
-
|
| 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()
|
| 101 |
-
|
| 102 |
-
if __name__ == "__main__":
|
| 103 |
-
main()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
|
| 4 |
+
# Base URL for LM Studio REST API
|
| 5 |
BASE_URL = "http://localhost:1234/api/v0"
|
| 6 |
|
| 7 |
+
# Chat completions function
|
| 8 |
+
def chat_with_lmstudio(messages, model, temperature=0.7, max_tokens=150):
|
| 9 |
+
endpoint = f"{BASE_URL}/chat/completions"
|
| 10 |
payload = {
|
| 11 |
+
"model": model,
|
| 12 |
"messages": messages,
|
| 13 |
+
"temperature": temperature,
|
| 14 |
+
"max_tokens": max_tokens,
|
| 15 |
"stream": False
|
| 16 |
}
|
| 17 |
+
try:
|
| 18 |
+
response = requests.post(endpoint, json=payload)
|
| 19 |
+
response.raise_for_status()
|
| 20 |
+
data = response.json()
|
| 21 |
+
return data["choices"][0]["message"]["content"]
|
| 22 |
+
except requests.RequestException as e:
|
| 23 |
+
return f"Error: {str(e)}"
|
| 24 |
|
| 25 |
+
# Text completions function
|
| 26 |
+
def text_completion(prompt, model, temperature=0.7, max_tokens=150):
|
| 27 |
+
endpoint = f"{BASE_URL}/completions"
|
| 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(endpoint, json=payload)
|
| 52 |
+
response.raise_for_status()
|
| 53 |
+
data = response.json()
|
| 54 |
+
return data["data"][0]["embedding"]
|
| 55 |
+
except requests.RequestException as e:
|
| 56 |
+
return f"Error: {str(e)}"
|
| 57 |
|
| 58 |
+
# Gradio Interface
|
| 59 |
+
def chat_interface(user_message, history, model="granite-3.0-2b-instruct"):
|
| 60 |
+
if history is None:
|
| 61 |
+
history = []
|
| 62 |
+
history.append({"role": "user", "content": user_message})
|
| 63 |
+
assistant_response = chat_with_lmstudio(history, model=model)
|
| 64 |
+
history.append({"role": "assistant", "content": assistant_response})
|
| 65 |
+
conversation = [(h["content"], history[i+1]["content"]) for i, h in enumerate(history[:-1]) if h["role"] == "user"]
|
| 66 |
+
return conversation, history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
+
with gr.Blocks() as demo:
|
| 69 |
+
gr.Markdown("# LM Studio API Interface")
|
| 70 |
|
| 71 |
+
with gr.Tab("Chat with Model"):
|
| 72 |
+
chat_history = gr.State([])
|
| 73 |
+
chat_model = gr.Textbox(value="granite-3.0-2b-instruct", label="Model")
|
| 74 |
+
chatbot = gr.Chatbot()
|
| 75 |
+
msg = gr.Textbox(placeholder="Enter your message", label="User Input")
|
| 76 |
+
submit_btn = gr.Button("Send")
|
| 77 |
+
submit_btn.click(chat_interface, inputs=[msg, chat_history, chat_model], outputs=[chatbot, chat_history])
|
|
|
|
| 78 |
|
| 79 |
+
with gr.Tab("Text Completion"):
|
| 80 |
+
completion_prompt = gr.Textbox(placeholder="Enter a prompt for text completion", label="Prompt")
|
| 81 |
+
completion_model = gr.Textbox(value="granite-3.0-2b-instruct", label="Model")
|
| 82 |
+
completion_output = gr.Textbox(label="Completion")
|
| 83 |
+
generate_btn = gr.Button("Generate")
|
| 84 |
+
generate_btn.click(text_completion, inputs=[completion_prompt, completion_model], outputs=completion_output)
|
|
|
|
|
|
|
| 85 |
|
| 86 |
+
with gr.Tab("Text Embeddings"):
|
| 87 |
+
embedding_text = gr.Textbox(placeholder="Enter text for embeddings", label="Input Text")
|
| 88 |
+
embedding_model = gr.Textbox(value="text-embedding-nomic-embed-text-v1.5", label="Model")
|
| 89 |
+
embedding_output = gr.JSON(label="Embeddings")
|
| 90 |
+
embed_btn = gr.Button("Get Embeddings")
|
| 91 |
+
embed_btn.click(text_embedding, inputs=[embedding_text, embedding_model], outputs=embedding_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
+
demo.launch(share=True)
|
|
|
|
|
|
|
|
|