abdeljalilELmajjodi commited on
Commit
69b7702
·
verified ·
1 Parent(s): e822530

update main app

Browse files
Files changed (1) hide show
  1. app.py +37 -122
app.py CHANGED
@@ -1,127 +1,8 @@
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
 
@@ -134,7 +15,41 @@ with gr.Blocks() as demo:
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")
 
1
+ from utils import *
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  with gr.Blocks() as demo:
4
  gr.Markdown("""
5
+ <h1 style="text-align: center;">Mohadata: Debate Data Generator 🤖 🇲🇦</h1>
6
 
7
  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.
8
 
 
15
  * download the conversation by clicking the **"Download Conversation"** button.
16
 
17
  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.
18
+ <details>
19
+ <summary style="text-align: center; font-size: 1.2em; font-weight: bold; color: #2196F3; cursor: pointer;">
20
+ 📋 Example Models and Base URLs
21
+ </summary>
22
+
23
+ <div style="display: flex; justify-content: center; margin: 20px 0;">
24
+ <table style="border-collapse: collapse; width: 80%; margin: auto;">
25
+ <thead>
26
+ <tr style="background-color: #2196F3; color: white;">
27
+ <th style="padding: 12px; text-align: left; border: 1px solid #ddd;">Model Name</th>
28
+ <th style="padding: 12px; text-align: left; border: 1px solid #ddd;">Base URL</th>
29
+ </tr>
30
+ </thead>
31
+ <tbody>
32
+ <tr style="background-color: #f5f5f5;">
33
+ <td style="padding: 12px; border: 1px solid #ddd;">claude-3</td>
34
+ <td style="padding: 12px; border: 1px solid #ddd;">https://api.anthropic.com/v1</td>
35
+ </tr>
36
+ <tr>
37
+ <td style="padding: 12px; border: 1px solid #ddd;">gpt-4</td>
38
+ <td style="padding: 12px; border: 1px solid #ddd;">https://api.openai.com/v1</td>
39
+ </tr>
40
+ <tr style="background-color: #f5f5f5;">
41
+ <td style="padding: 12px; border: 1px solid #ddd;">gemini-1.5-pro</td>
42
+ <td style="padding: 12px; border: 1px solid #ddd;">https://generativelanguage.googleapis.com/v1beta</td>
43
+ </tr>
44
+ <tr>
45
+ <td style="padding: 12px; border: 1px solid #ddd;">deepseek-chat</td>
46
+ <td style="padding: 12px; border: 1px solid #ddd;">https://api.deepseek.com</td>
47
+ </tr>
48
+ </tbody>
49
+ </table>
50
+ </div>
51
+ </details>
52
+ """)
53
  with gr.Row("compact"):
54
  model_name=gr.Textbox(label="Model Name",placeholder="Enter Model Name")
55
  base_url=gr.Textbox(label="Base URL",placeholder="Enter Base URL")