Spaces:
Runtime error
Runtime error
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)
|