Aborman commited on
Commit
007f371
·
verified ·
1 Parent(s): 542f845

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. 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 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)
67
- chat_interface.launch()
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()
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()
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()
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)