File size: 5,351 Bytes
7e920cb
 
 
 
 
 
 
 
 
 
 
 
 
 
207f049
7e920cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
207f049
7e920cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import gradio as gr
from openai import OpenAI
import os
from tqdm import tqdm
import pandas as pd
from pathlib import Path
from datasets import Dataset,load_dataset,concatenate_datasets
import asyncio
import threading
from dotenv import load_dotenv

load_dotenv()
HF_READ=os.environ["HF_READ"]
HF_WRITE=os.environ["HF_WRITE"]
print(HF_READ,HF_WRITE)
model_base_url={}
LANGUAGE="MOROCCAN Arabic"
HF_DATASET="abdeljalilELmajjodi/Mohadata"
SYSTEM_PROMPT = {
            "role": "system",
            "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}.

            QUESTIONER_AI:
            - Must only ask questions that can be answered from the provided context
            - Should identify key information gaps or unclear points
            - Cannot ask questions about information not present in the context
            - Must communicate exclusively in {LANGUAGE}

            ANSWERER_AI:
            - Must only answer using information explicitly stated in the context
            - Cannot add external information or assumptions
            - Must indicate if a question cannot be answered from the context alone
            - Must communicate exclusively in {LANGUAGE}"""
        }

def add_model(model_name,base_url,api_key):
    model_base_url[model_name]=base_url
    #model_quest.choices=list(model_base_url.keys())
    #print(model_quest)
    os.environ[model_name]=api_key
    return gr.Dropdown(label="Questioner Model",choices=list(model_base_url.keys())),gr.Dropdown(label="Answerer Model",choices=list(model_base_url.keys()))


def model_init(model):
    try:
        api_key=os.environ.get(model)
        base_url=model_base_url[model]
        client = OpenAI(api_key=api_key, base_url=base_url)
        return client
    except Exception as e:
         print(f"You should add api key of {model}")

# generate questions
def init_req_messages(sample_context):
  messages_quest=[
      SYSTEM_PROMPT,
      {
            "role":"user",
            "content":f"""Context for analysis:
            {sample_context}
            As QUESTIONER_AI, generate a question based on this context.
            """
      }
  ]
  return messages_quest
# generate Answers
def init_resp_messages(sample_context,question):
  messages_answ=[
      SYSTEM_PROMPT,
      {
          "role": "user",
          "content": f"""
          Context for analysis:
          {sample_context}
          Question: {question}
          As ANSWERER_AI, answer this question using only information from the context.
          """}

  ]
  return messages_answ

def chat_generation(client,model_name,messages):
  return client.chat.completions.create(
    model=model_name,
    messages=messages,
    temperature=0.5
    ).choices[0].message.content

def generate_question(client,model_name,messages_quest):
  question=chat_generation(client,model_name,messages_quest)
  messages_quest.append({"role":"assistant","content":question})
  return question

def generate_answer(client,model_name,messages_answ):
  answer=chat_generation(client,model_name,messages_answ)
  messages_answ.append({"role":"assistant","content":answer})
  return answer

def load_upload_ds_hf(df):
    dataset_stream=load_dataset("atlasia/Mohadata_Dataset",token=HF_READ,split="train")
    print("[INFO] dataset loaded successfully")
    new_ds=Dataset.from_pandas(df,preserve_index=False)
    updated_ds=concatenate_datasets([dataset_stream,new_ds])
    updated_ds.push_to_hub("atlasia/Mohadata_Dataset",token=HF_WRITE)
    print("[INFO] dataset uploaded successfully")

async def load_upload_ds_hf_async(df):
    await asyncio.to_thread(load_upload_ds_hf,df)
    


def save_conversation(conversation,context,num_rounds):
    conv_flat={"user":[],"assistant":[]}
    for i in range(0,len(conversation)):
        conv_flat[conversation[i]["role"]].append(conversation[i]["content"])
    conv_flat["context"]=[context]*num_rounds
    df=pd.DataFrame(conv_flat)
    df.to_csv("data.csv")
    print("[INFO] conversation saved successfully")
    print("[INFO] uploading dataset to huggingface")
    
    thread=threading.Thread(target=load_upload_ds_hf,args=(df,))
    thread.daemon=True
    thread.start()

    return Path("data.csv").name

def user_input(context,model_a,model_b,num_rounds,conversation_history):
    conversation_history.clear()
    client_quest=model_init(model_a)
    client_ans=model_init(model_b)
    messages_quest=init_req_messages(context)
    for round_num in tqdm(range(num_rounds)):
            question = generate_question(client_quest,model_a,messages_quest)
            conversation_history.append(
                {"role":"user","content":question},
            )
            if round_num==0:
              messages_answ=init_resp_messages(context,question)
            else:
              messages_answ.append({"role":"user","content":question})
            answer = generate_answer(client_ans,model_b,messages_answ)
            messages_quest.append({"role":"user","content":answer})
            conversation_history.append(
                {"role":"assistant","content":answer},
            )
    file_path=save_conversation(conversation_history,context,num_rounds)
    return conversation_history,gr.DownloadButton(label="Save Conversation",value=file_path,visible=True)