Spaces:
Runtime error
Runtime error
Royrotem100
commited on
Commit
โข
b228d02
1
Parent(s):
2b47d91
Initial commit
Browse files- app.py +112 -0
- app_api.py +60 -0
app.py
ADDED
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
import requests
|
4 |
+
from typing import List, Dict, Tuple
|
5 |
+
|
6 |
+
# Define the API URL (adjust according to your server address)
|
7 |
+
API_URL = "http://127.0.0.1:5000/chat"
|
8 |
+
|
9 |
+
History = List[Tuple[str, str]]
|
10 |
+
Messages = List[Dict[str, str]]
|
11 |
+
|
12 |
+
def clear_session() -> History:
|
13 |
+
return []
|
14 |
+
|
15 |
+
def history_to_messages(history: History) -> Messages:
|
16 |
+
messages = []
|
17 |
+
for h in history:
|
18 |
+
messages.append({'role': 'user', 'content': h[0].strip()})
|
19 |
+
messages.append({'role': 'assistant', 'content': h[1].strip()})
|
20 |
+
return messages
|
21 |
+
|
22 |
+
def messages_to_history(messages: Messages) -> History:
|
23 |
+
history = []
|
24 |
+
for q, r in zip(messages[0::2], messages[1::2]):
|
25 |
+
history.append((q['content'], r['content']))
|
26 |
+
return history
|
27 |
+
|
28 |
+
def model_chat(query: str, history: History) -> Tuple[str, History]:
|
29 |
+
if not query.strip():
|
30 |
+
return '', history
|
31 |
+
|
32 |
+
messages = history_to_messages(history)
|
33 |
+
messages.append({'role': 'user', 'content': query.strip()})
|
34 |
+
|
35 |
+
response = requests.post(API_URL, json={"messages": messages})
|
36 |
+
if response.status_code != 200:
|
37 |
+
return "Error: Failed to get response from the API", history
|
38 |
+
|
39 |
+
response_json = response.json()
|
40 |
+
response_text = response_json["response"]
|
41 |
+
|
42 |
+
history.append((query.strip(), response_text.strip()))
|
43 |
+
return response_text.strip(), history
|
44 |
+
|
45 |
+
with gr.Blocks(css='''
|
46 |
+
.gr-group {direction: rtl;}
|
47 |
+
.chatbot{text-align:right;}
|
48 |
+
.dicta-header {
|
49 |
+
background-color: var(--input-background-fill);
|
50 |
+
border-radius: 10px;
|
51 |
+
padding: 20px;
|
52 |
+
text-align: center;
|
53 |
+
display: flex;
|
54 |
+
flex-direction: row;
|
55 |
+
align-items: center;
|
56 |
+
box-shadow: var(--block-shadow);
|
57 |
+
border-color: var(--block-border-color);
|
58 |
+
border-width: 1px;
|
59 |
+
}
|
60 |
+
@media (max-width: 768px) {
|
61 |
+
.dicta-header {
|
62 |
+
flex-direction: column;
|
63 |
+
}
|
64 |
+
}
|
65 |
+
.chatbot.prose {
|
66 |
+
font-size: 1.2em;
|
67 |
+
}
|
68 |
+
.dicta-logo {
|
69 |
+
width: 150px;
|
70 |
+
height: auto;
|
71 |
+
margin-bottom: 20px;
|
72 |
+
}
|
73 |
+
.dicta-intro-text {
|
74 |
+
margin-bottom: 20px;
|
75 |
+
text-align: center;
|
76 |
+
display: flex;
|
77 |
+
flex-direction: column;
|
78 |
+
align-items: center;
|
79 |
+
width: 100%;
|
80 |
+
font-size: 1.1em;
|
81 |
+
}
|
82 |
+
textarea {
|
83 |
+
font-size: 1.2em;
|
84 |
+
}
|
85 |
+
''', js=None) as demo:
|
86 |
+
gr.Markdown("""
|
87 |
+
<div class="dicta-header">
|
88 |
+
<a href="">
|
89 |
+
<img src="\\logo111.png" alt="Logo" class="dicta-logo">
|
90 |
+
</a>
|
91 |
+
<div class="dicta-intro-text">
|
92 |
+
<h1>ืฆ'ืื ืืขืจืื - ืืืืื ืจืืฉืื ืืช</h1>
|
93 |
+
<span dir='rtl'>ืืจืืืื ืืืืื ืืืื ืืืื ืืจืืงืืืื ืืจืืฉืื. ืืงืจื ืืช ืืืืืืช ืืืืื ืืจืื ืืืฆื ืืื ืืืื ืืกืืืข ืืื ืืืฉืืืืชืืื</span><br/>
|
94 |
+
<span dir='rtl'>ืืืื ื ืืชื ืขื ืืื ืกืจื ืจืืขื ืจืชื ืชืื ืฉืืืืฉ ืืืืื ืฉืคื ืืืงืื ืฉืคืืชื ืขื ืืื ืืคื"ืช</span><br/>
|
95 |
+
</div>
|
96 |
+
</div>
|
97 |
+
""")
|
98 |
+
|
99 |
+
chatbot = gr.Chatbot(height=600)
|
100 |
+
query = gr.Textbox(placeholder="ืืื ืก ืฉืืื ืืขืืจืืช (ืื ืืื ืืืืช!)", rtl=True)
|
101 |
+
clear_btn = gr.Button("ื ืงื ืฉืืื")
|
102 |
+
|
103 |
+
def respond(query, history):
|
104 |
+
response, history = model_chat(query, history)
|
105 |
+
return history, gr.update(value="", interactive=True)
|
106 |
+
|
107 |
+
demo_state = gr.State([])
|
108 |
+
|
109 |
+
query.submit(respond, [query, demo_state], [chatbot, query, demo_state])
|
110 |
+
clear_btn.click(clear_session, [], demo_state, chatbot)
|
111 |
+
|
112 |
+
demo.launch(share=True)
|
app_api.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
import torch
|
4 |
+
|
5 |
+
app = Flask(__name__)
|
6 |
+
|
7 |
+
# Load the model and tokenizer
|
8 |
+
model_name = "dicta-il/dictalm2.0-instruct"
|
9 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16)
|
10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
11 |
+
|
12 |
+
# Ensure the tokenizer has a pad token, if not, add one.
|
13 |
+
if tokenizer.pad_token is None:
|
14 |
+
tokenizer.add_special_tokens({'pad_token': '[PAD]'})
|
15 |
+
model.resize_token_embeddings(len(tokenizer))
|
16 |
+
|
17 |
+
# Set the device to load the model onto
|
18 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
19 |
+
model.to(device)
|
20 |
+
|
21 |
+
@app.route('/chat', methods=['POST'])
|
22 |
+
def chat():
|
23 |
+
data = request.json
|
24 |
+
messages = data.get("messages", [])
|
25 |
+
|
26 |
+
if not messages:
|
27 |
+
return jsonify({"error": "No messages provided"}), 400
|
28 |
+
|
29 |
+
# Combine messages into a single input string with the correct template
|
30 |
+
conversation = "<s>"
|
31 |
+
for i, message in enumerate(messages):
|
32 |
+
role = message["role"]
|
33 |
+
content = message["content"]
|
34 |
+
if role == "user":
|
35 |
+
if i == 0:
|
36 |
+
conversation += f"[INST] {content} [/INST]"
|
37 |
+
else:
|
38 |
+
conversation += f" [INST] {content} [/INST]"
|
39 |
+
elif role == "assistant":
|
40 |
+
conversation += f" {content}"
|
41 |
+
conversation += "</s>"
|
42 |
+
|
43 |
+
# Tokenize the combined conversation
|
44 |
+
encoded = tokenizer(conversation, return_tensors="pt").to(device)
|
45 |
+
|
46 |
+
# Generate response using the model
|
47 |
+
generated_ids = model.generate(
|
48 |
+
input_ids=encoded['input_ids'],
|
49 |
+
attention_mask=encoded['attention_mask'],
|
50 |
+
max_new_tokens=50,
|
51 |
+
pad_token_id=tokenizer.pad_token_id,
|
52 |
+
do_sample=True
|
53 |
+
)
|
54 |
+
|
55 |
+
decoded = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
|
56 |
+
|
57 |
+
return jsonify({"response": decoded})
|
58 |
+
|
59 |
+
if __name__ == '__main__':
|
60 |
+
app.run(host='0.0.0.0', port=5000)
|