abdeljalilELmajjodi commited on
Commit
3f08ea9
·
verified ·
1 Parent(s): 4348c74

first commit of the app

Browse files
Files changed (1) hide show
  1. app.py +169 -0
app.py ADDED
@@ -0,0 +1,169 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from openai import OpenAI
3
+ import os
4
+ from tqdm import tqdm
5
+ import pandas as pd
6
+ from pathlib import Path
7
+
8
+ model_base_url={}
9
+
10
+ language="MOROCCAN Arabic"
11
+ SYSTEM_PROMPT = {
12
+ "role": "system",
13
+ "content": f"""This is a context-based Q&A game where two AIs interact with a user-provided context. All interactions MUST be in {language}.
14
+
15
+ QUESTIONER_AI:
16
+ - Must only ask questions that can be answered from the provided context
17
+ - Should identify key information gaps or unclear points
18
+ - Must quote or reference specific parts of the context
19
+ - Cannot ask questions about information not present in the context
20
+ - Must communicate exclusively in {language}
21
+
22
+ ANSWERER_AI:
23
+ - Must only answer using information explicitly stated in the context
24
+ - Cannot add external information or assumptions
25
+ - Must indicate if a question cannot be answered from the context alone
26
+ - Must communicate exclusively in {language}"""
27
+ }
28
+
29
+ def add_model(model_name,base_url,api_key):
30
+ model_base_url[model_name]=base_url
31
+ model_quest.choices=list(model_base_url.keys())
32
+ os.environ[model_name]=api_key
33
+ return gr.Dropdown(label="Questioner Model",choices=list(model_base_url.keys())),gr.Dropdown(label="Answerer Model",choices=list(model_base_url.keys()))
34
+
35
+
36
+ def model_init(model):
37
+ try:
38
+ api_key=os.environ.get(model)
39
+ base_url=model_base_url[model]
40
+ client = OpenAI(api_key=api_key, base_url=base_url)
41
+ return client
42
+ except Exception as e:
43
+ print(f"You should add api key of {model}")
44
+
45
+ # generate questions
46
+ def init_req_messages(sample_context):
47
+ messages_quest=[
48
+ SYSTEM_PROMPT,
49
+ {
50
+ "role":"user",
51
+ "content":f"""Context for analysis:
52
+ {sample_context}
53
+ As QUESTIONER_AI, generate a question based on this context.
54
+ """
55
+ }
56
+ ]
57
+ return messages_quest
58
+ # generate Answers
59
+ def init_resp_messages(sample_context,question):
60
+ messages_answ=[
61
+ SYSTEM_PROMPT,
62
+ {
63
+ "role": "user",
64
+ "content": f"""
65
+ Context for analysis:
66
+ {sample_context}
67
+ Question: {question}
68
+ As ANSWERER_AI, answer this question using only information from the context.
69
+ """}
70
+
71
+ ]
72
+ return messages_answ
73
+
74
+ def chat_generation(client,model_name,messages):
75
+ return client.chat.completions.create(
76
+ model=model_name,
77
+ messages=messages,
78
+ temperature=0.5
79
+ ).choices[0].message.content
80
+
81
+ def generate_question(client,model_name,messages_quest):
82
+ question=chat_generation(client,model_name,messages_quest)
83
+ messages_quest.append({"role":"assistant","content":question})
84
+ return question
85
+
86
+ def generate_answer(client,model_name,messages_answ):
87
+ answer=chat_generation(client,model_name,messages_answ)
88
+ messages_answ.append({"role":"assistant","content":answer})
89
+ return answer
90
+
91
+ def save_conversation(conversation):
92
+ conv_flat={"user":[],"assistant":[]}
93
+ for i in range(0,len(conversation)):
94
+ conv_flat[conversation[i]["role"]].append(conversation[i]["content"])
95
+ df=pd.DataFrame(conv_flat)
96
+ df.to_csv("data.csv")
97
+ return Path("data.csv").name
98
+
99
+ def user_input(context,model_a,model_b,num_rounds,conversation_history):
100
+ conversation_history.clear()
101
+ client_quest=model_init(model_a)
102
+ client_ans=model_init(model_b)
103
+ messages_quest=init_req_messages(context)
104
+ for round_num in tqdm(range(num_rounds)):
105
+ question = generate_question(client_quest,model_a,messages_quest)
106
+ conversation_history.append(
107
+ {"role":"user","content":question},
108
+ )
109
+ if round_num==0:
110
+ messages_answ=init_resp_messages(context,question)
111
+ else:
112
+ messages_answ.append({"role":"user","content":question})
113
+ answer = generate_answer(client_ans,model_b,messages_answ)
114
+ messages_quest.append({"role":"user","content":answer})
115
+ conversation_history.append(
116
+ {"role":"assistant","content":answer},
117
+ )
118
+ file_path=save_conversation(conversation_history)
119
+
120
+ return conversation_history,gr.DownloadButton(label="Save Conversation",value=file_path,visible=True)
121
+
122
+ with gr.Blocks() as demo:
123
+ gr.Markdown("""
124
+ <h1 style="text-align: center;">Mohadata: Debate Data Generator 🤖</h1>
125
+
126
+ This tool generates a debate-style conversation between two AI models based on a given **context**. It simulates a question-answer dialogue, where one model acts as the questioner and the other as the answerer. The conversation is generated iteratively, with each model responding to the previous message from the other model.
127
+
128
+ To use this tool:
129
+ * First, add information about the models you want to use by clicking the "+" button and filling in the required details.
130
+ * simply enter the **context** of the debate in the provided text box.
131
+ * select the models you want to use for the **questioner** and **answerer**.
132
+ * specify the **number of rounds** you want the conversation to last.
133
+ * click the **"Submit"** button to generate the conversation.
134
+ * download the conversation by clicking the **"Download Conversation"** button.
135
+
136
+ The conversation will be displayed in the chatbot window, with the questioner's messages on the right and the answerer's messages on the left. This tool can be useful for generating debate-style conversations on a given topic, and can help in understanding different perspectives and arguments on a particular issue.
137
+ """)
138
+ with gr.Row("compact"):
139
+ model_name=gr.Textbox(label="Model Name",placeholder="Enter Model Name")
140
+ base_url=gr.Textbox(label="Base URL",placeholder="Enter Base URL")
141
+ api_key=gr.Textbox(label="API Key",placeholder="Enter API Key",type="password")
142
+ add=gr.Button("+",variant="huggingface")
143
+ with gr.Row(equal_height=True):
144
+ context=gr.Textbox(label="context",lines=3)
145
+ with gr.Row():
146
+ model_quest=gr.Dropdown(label="Questioner Model",choices=list(model_base_url.keys()))
147
+ model_answ=gr.Dropdown(label="Answerer Model",choices=list(model_base_url.keys()))
148
+ num_rounds=gr.Number(label="Number Rounds",minimum=1)
149
+ with gr.Row():
150
+ submit=gr.Button("Submit",variant="primary")
151
+ with gr.Row():
152
+ chatbot=gr.Chatbot(
153
+ type="messages",rtl=True
154
+ )
155
+ with gr.Row():
156
+ save=gr.DownloadButton(label="Download Conversation",visible=False)
157
+ add.click(
158
+ add_model,
159
+ inputs=[model_name,base_url,api_key],
160
+ outputs=[model_quest,model_answ]
161
+ )
162
+ submit.click(
163
+ user_input,
164
+ inputs=[context,model_quest,model_answ,num_rounds,chatbot],
165
+ outputs=[chatbot,save])
166
+
167
+
168
+
169
+ demo.launch()