Spaces:
Sleeping
Sleeping
Sébastien De Greef
commited on
Commit
·
861a9e6
1
Parent(s):
d473058
Update main.py and start_server.sh scripts
Browse files- main.py +46 -19
- start_server.sh +2 -2
main.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
from langchain.schema import AIMessage, HumanMessage
|
2 |
import gradio as gr
|
3 |
from langchain_community.llms import Ollama
|
4 |
-
|
5 |
def parse_model_names(path):
|
6 |
"""Parses the model file to extract value-label pairs for the dropdown."""
|
7 |
choices = []
|
@@ -19,39 +19,66 @@ models = parse_model_names("models.txt")
|
|
19 |
|
20 |
|
21 |
def predict(message, history, model):
|
22 |
-
print("Predicting", message, history,
|
23 |
llm = Ollama(model=models[model][1], timeout=1000) # Instantiate Ollama with the selected model
|
24 |
history_langchain_format = []
|
25 |
-
for
|
26 |
-
history_langchain_format.append(HumanMessage(content=
|
27 |
-
|
28 |
-
|
29 |
try:
|
30 |
chat_response = llm.invoke(history_langchain_format)
|
31 |
except Exception as e: # Use a general exception handler here
|
32 |
chat_response = "Error: " + str(e)
|
33 |
-
|
34 |
-
return chat_response
|
35 |
|
36 |
|
37 |
|
38 |
-
with gr.Blocks(fill_height=True) as demo:
|
39 |
-
|
40 |
-
model_dropdown = gr.Dropdown(label="Select LLM Model", choices=models, info="Select the model you want to chat with", type="index")
|
41 |
|
42 |
-
# We use a state variable to track the current model
|
43 |
-
model_state = gr.State(value=model_dropdown.value)
|
44 |
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
|
50 |
|
51 |
-
|
52 |
-
|
53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
|
57 |
if __name__ == "__main__":
|
|
|
1 |
from langchain.schema import AIMessage, HumanMessage
|
2 |
import gradio as gr
|
3 |
from langchain_community.llms import Ollama
|
4 |
+
import time
|
5 |
def parse_model_names(path):
|
6 |
"""Parses the model file to extract value-label pairs for the dropdown."""
|
7 |
choices = []
|
|
|
19 |
|
20 |
|
21 |
def predict(message, history, model):
|
22 |
+
print("Predicting", message, history, model),
|
23 |
llm = Ollama(model=models[model][1], timeout=1000) # Instantiate Ollama with the selected model
|
24 |
history_langchain_format = []
|
25 |
+
for m in message:
|
26 |
+
history_langchain_format.append(HumanMessage(content=m[0]))
|
27 |
+
if m[1] is not None:
|
28 |
+
history_langchain_format.append(AIMessage(content=m[1]))
|
29 |
try:
|
30 |
chat_response = llm.invoke(history_langchain_format)
|
31 |
except Exception as e: # Use a general exception handler here
|
32 |
chat_response = "Error: " + str(e)
|
33 |
+
|
34 |
+
return [(chat_response, )]
|
35 |
|
36 |
|
37 |
|
38 |
+
# with gr.Blocks(fill_height=True) as demo:
|
39 |
+
# with gr.Row():
|
|
|
40 |
|
|
|
|
|
41 |
|
42 |
+
# def update_model(selected_model):
|
43 |
+
# print("Model selected", selected_model)
|
44 |
+
# model_state.value = selected_model
|
45 |
+
# return selected_model
|
46 |
|
47 |
|
48 |
+
# chat = gr.ChatInterface(predict,
|
49 |
+
# additional_inputs=[ model_dropdown ],
|
50 |
|
51 |
+
# )
|
52 |
+
|
53 |
+
|
54 |
+
def print_like_dislike(x: gr.LikeData):
|
55 |
+
print(x.index, x.value, x.liked)
|
56 |
+
|
57 |
+
def add_message(history, message):
|
58 |
+
for x in message["files"]:
|
59 |
+
history.append(((x,), None))
|
60 |
+
if message["text"] is not None:
|
61 |
+
history.append((message["text"], None))
|
62 |
+
return history, gr.MultimodalTextbox(value=None, interactive=False)
|
63 |
+
|
64 |
+
with gr.Blocks() as demo:
|
65 |
+
model_dropdown = gr.Dropdown(label="Select LLM Model", choices=models, info="Select the model you want to chat with", type="index")
|
66 |
+
model_state = gr.State(value=model_dropdown.value)
|
67 |
+
chatbot = gr.Chatbot(
|
68 |
+
[],
|
69 |
+
elem_id="chatbot",
|
70 |
+
bubble_full_width=False
|
71 |
)
|
72 |
+
|
73 |
+
chat_input = gr.MultimodalTextbox(interactive=True, file_types=["image"], placeholder="Enter message or upload file...", show_label=False)
|
74 |
+
|
75 |
+
chat_msg = chat_input.submit(add_message, [chatbot, chat_input], [chatbot, chat_input])
|
76 |
+
bot_msg = chat_msg.then(predict, [chatbot, chat_input, model_dropdown], chatbot, api_name="bot_response")
|
77 |
+
bot_msg.then(lambda: gr.MultimodalTextbox(interactive=True), None, [chat_input])
|
78 |
+
|
79 |
+
chatbot.like(print_like_dislike, None, None)
|
80 |
+
|
81 |
+
demo.queue()
|
82 |
|
83 |
|
84 |
if __name__ == "__main__":
|
start_server.sh
CHANGED
@@ -6,7 +6,7 @@ ollama pull llama3:8b > /dev/null 2>&1
|
|
6 |
ollama pull gemma:2b > /dev/null 2>&1
|
7 |
ollama pull gemma:7b > /dev/null 2>&1
|
8 |
|
9 |
-
ollama create mistral4k:7b --file
|
10 |
-
ollama create llama38k:8b
|
11 |
|
12 |
python main.py
|
|
|
6 |
ollama pull gemma:2b > /dev/null 2>&1
|
7 |
ollama pull gemma:7b > /dev/null 2>&1
|
8 |
|
9 |
+
ollama create mistral4k:7b --file /home/user/app/mistral7b.Modelfile
|
10 |
+
ollama create llama38k:8b --file /home/user/app/llama38b.Modelfile
|
11 |
|
12 |
python main.py
|