hadihosseini0171
commited on
Commit
•
238795e
1
Parent(s):
0877410
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,154 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
import requests
|
5 |
+
|
6 |
+
|
7 |
+
BACKEND_ADDR = "http://46.8.230.12:8585/post"
|
8 |
+
|
9 |
+
# Initialize an empty list to store user queries
|
10 |
+
queries_list = []
|
11 |
+
|
12 |
+
# Convert model name to the corresponding model id that backend understands
|
13 |
+
llm_name2id = {
|
14 |
+
"Llama-3.1-70B-Versatile": "llama-3.1-70b-versatile",
|
15 |
+
"Llama-3-70B-8192": "llama3-70b-8192",
|
16 |
+
"Llama-3-8B-8192": "llama3-8b-8192"
|
17 |
+
}
|
18 |
+
|
19 |
+
# Some default values
|
20 |
+
DEFAULT_TEMP = 0.2
|
21 |
+
DEFAULT_MODEL = "Llama-3-70B-8192"
|
22 |
+
DEFAULT_USE_RAG = True
|
23 |
+
|
24 |
+
|
25 |
+
# Function to handle the 'Clear' button
|
26 |
+
def clear_queries():
|
27 |
+
global queries_list
|
28 |
+
|
29 |
+
queries_list = []
|
30 |
+
return "", DEFAULT_MODEL, DEFAULT_TEMP, DEFAULT_USE_RAG, "", ""
|
31 |
+
|
32 |
+
|
33 |
+
# Function to handle the 'Add' button
|
34 |
+
def add_to_list(query_txt, model, temperature, use_rag):
|
35 |
+
global queries_list
|
36 |
+
|
37 |
+
if len(query_txt) > 0:
|
38 |
+
queries_list.append(
|
39 |
+
{
|
40 |
+
"prompt": query_txt,
|
41 |
+
"temperature": str(temperature),
|
42 |
+
"model": llm_name2id[model],
|
43 |
+
"use_rag": str(use_rag),
|
44 |
+
}
|
45 |
+
)
|
46 |
+
|
47 |
+
return "", generate_queries_str(queries_list)
|
48 |
+
|
49 |
+
|
50 |
+
# Function to handle the 'Submit' button
|
51 |
+
def submit(query_txt, model, temperature, use_rag):
|
52 |
+
global queries_list
|
53 |
+
|
54 |
+
if len(query_txt) > 0:
|
55 |
+
_, queries = add_to_list(query_txt, model, temperature, use_rag)
|
56 |
+
else:
|
57 |
+
queries = generate_queries_str(queries_list)
|
58 |
+
|
59 |
+
if len(queries_list) > 0:
|
60 |
+
response = requests.post(BACKEND_ADDR, json=queries_list)
|
61 |
+
answers = generate_answers_str(response.json())
|
62 |
+
|
63 |
+
# Re-initialize the user's query list
|
64 |
+
queries_list = []
|
65 |
+
else:
|
66 |
+
answers = ""
|
67 |
+
|
68 |
+
return "", queries, answers
|
69 |
+
|
70 |
+
|
71 |
+
# Helper function to generate string representation of user queries
|
72 |
+
def generate_queries_str(queries_list):
|
73 |
+
delimiter = f"\n{'-' * 120}\n"
|
74 |
+
queries_str = delimiter.join([q["prompt"] for q in queries_list])
|
75 |
+
return queries_str
|
76 |
+
|
77 |
+
|
78 |
+
# Helper function to generate string representation of model answers
|
79 |
+
def generate_answers_str(answers_list):
|
80 |
+
delimiter = f"\n{'-' * 120}\n"
|
81 |
+
answers_str = delimiter.join([clean(a["answer"]) for a in answers_list])
|
82 |
+
return answers_str
|
83 |
+
|
84 |
+
|
85 |
+
# Helper function to clean a model-generated answer
|
86 |
+
def clean(answer_str):
|
87 |
+
answer_str = re.sub('^\s*:', '', answer_str)
|
88 |
+
|
89 |
+
garbages = [
|
90 |
+
"Here is the generated paragraph:",
|
91 |
+
"Let me know if this meets your requirements!",
|
92 |
+
]
|
93 |
+
for g in garbages:
|
94 |
+
answer_str = answer_str.replace(g, "")
|
95 |
+
answer_str = answer_str.strip()
|
96 |
+
return answer_str
|
97 |
+
|
98 |
+
|
99 |
+
if __name__ == "__main__":
|
100 |
+
# Gradio interface
|
101 |
+
with gr.Blocks(theme=gr.themes.Default()) as demo:
|
102 |
+
with gr.Row():
|
103 |
+
with gr.Column(scale=2):
|
104 |
+
query_txt = gr.Textbox(
|
105 |
+
placeholder="پرسش خود را اینجا وارد کنید...",
|
106 |
+
label="Query", rtl=True)
|
107 |
+
with gr.Column(scale=1):
|
108 |
+
model = gr.Radio(
|
109 |
+
choices=[
|
110 |
+
"Llama-3-8B-8192",
|
111 |
+
"Llama-3-70B-8192",
|
112 |
+
"Llama-3.1-70B-Versatile",
|
113 |
+
],
|
114 |
+
value=DEFAULT_MODEL,
|
115 |
+
label="LLM"
|
116 |
+
)
|
117 |
+
use_rag = gr.Checkbox(value=DEFAULT_USE_RAG, label="Use RAG?")
|
118 |
+
temperature = gr.Slider(minimum=0, maximum=1,
|
119 |
+
value=DEFAULT_TEMP,
|
120 |
+
step=0.1, label="Temperature")
|
121 |
+
|
122 |
+
with gr.Row():
|
123 |
+
clear_btn = gr.Button("Clear", variant="stop")
|
124 |
+
add_btn = gr.Button("Add", variant="secondary")
|
125 |
+
submit_btn = gr.Button("Submit", variant="primary")
|
126 |
+
|
127 |
+
with gr.Row():
|
128 |
+
with gr.Column():
|
129 |
+
queries_box = gr.Textbox(
|
130 |
+
placeholder="پرسشهای شما اینجا نمایش داده خواهد شد...",
|
131 |
+
label="Queries", interactive=False, rtl=True)
|
132 |
+
with gr.Column():
|
133 |
+
answers_box = gr.Textbox(
|
134 |
+
placeholder="پاسخهای مدل اینجا نمایش داده خواهد شد...",
|
135 |
+
label="Answers", interactive=False, rtl=True)
|
136 |
+
|
137 |
+
clear_btn.click(
|
138 |
+
fn=clear_queries,
|
139 |
+
inputs=[],
|
140 |
+
outputs=[query_txt, model, temperature, use_rag,
|
141 |
+
queries_box, answers_box]
|
142 |
+
)
|
143 |
+
add_btn.click(
|
144 |
+
fn=add_to_list,
|
145 |
+
inputs=[query_txt, model, temperature, use_rag],
|
146 |
+
outputs=[query_txt, queries_box]
|
147 |
+
)
|
148 |
+
submit_btn.click(
|
149 |
+
fn=submit,
|
150 |
+
inputs=[query_txt, model, temperature, use_rag],
|
151 |
+
outputs=[query_txt, queries_box, answers_box]
|
152 |
+
)
|
153 |
+
|
154 |
+
demo.launch()
|