File size: 3,391 Bytes
542f845 465fe5b 542f845 465fe5b 542f845 465fe5b 542f845 465fe5b 542f845 465fe5b 542f845 465fe5b 542f845 465fe5b 542f845 465fe5b 542f845 465fe5b 542f845 465fe5b 542f845 465fe5b 542f845 465fe5b 542f845 465fe5b 542f845 465fe5b 542f845 465fe5b 542f845 465fe5b 542f845 465fe5b 542f845 007f371 465fe5b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 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 94 95 96 97 98 99 100 101 102 103 104 |
import gradio as gr
import requests
# LM Studio REST API base URL
BASE_URL = "http://localhost:1234/api/v0"
# Function to handle chat completions
def chat_with_lmstudio(messages):
url = f"{BASE_URL}/chat/completions"
payload = {
"model": "granite-3.0-2b-instruct", # Replace with the model you have loaded
"messages": messages,
"temperature": 0.7,
"max_tokens": 1024,
"stream": False
}
response = requests.post(url, json=payload)
response.raise_for_status()
response_data = response.json()
return response_data['choices'][0]['message']['content']
# Function to handle text completions
def get_text_completion(prompt):
url = f"{BASE_URL}/completions"
payload = {
"model": "granite-3.0-2b-instruct", # Replace with the model you have loaded
"prompt": prompt,
"temperature": 0.7,
"max_tokens": 100,
"stream": False
}
response = requests.post(url, json=payload)
response.raise_for_status()
response_data = response.json()
return response_data['choices'][0]['text']
# Function to handle text embeddings
def get_text_embedding(text):
url = f"{BASE_URL}/embeddings"
payload = {
"model": "text-embedding-nomic-embed-text-v1.5", # Replace with your embedding model
"input": text
}
response = requests.post(url, json=payload)
response.raise_for_status()
response_data = response.json()
return response_data['data'][0]['embedding']
# Gradio interface for chat
def gradio_chat_interface():
def chat_interface(user_input, history):
# Format history in LM Studio messages format
messages = []
for user_msg, assistant_msg in history:
messages.append({"role": "user", "content": user_msg})
messages.append({"role": "assistant", "content": assistant_msg})
messages.append({"role": "user", "content": user_input})
# Get response from LM Studio
response = chat_with_lmstudio(messages)
# Update history with the assistant's response
history.append((user_input, response))
return history, history
chat_interface = gr.ChatInterface(chat_interface, type='messages')
chat_interface.launch(share=True)
# Gradio interface for text completion
def gradio_text_completion():
gr.Interface(
fn=get_text_completion,
inputs="text",
outputs="text",
title="Text Completion with LM Studio"
).launch(share=True)
# Gradio interface for text embedding
def gradio_text_embedding():
gr.Interface(
fn=get_text_embedding,
inputs="text",
outputs="text",
title="Text Embedding with LM Studio"
).launch(share=True)
# Main menu to choose the interface
def main():
with gr.Blocks() as demo:
gr.Markdown("""
# LM Studio API Interface
Choose which function you want to use with LM Studio:
""")
with gr.Row():
gr.Button("Chat with Model").click(gradio_chat_interface)
gr.Button("Text Completion").click(gradio_text_completion)
gr.Button("Text Embedding").click(gradio_text_embedding)
demo.launch(share=True)
if __name__ == "__main__":
main()
|