Sameercodes commited on
Commit
eab216c
·
verified ·
1 Parent(s): 06a2a0d

Upload 6 files

Browse files
Files changed (6) hide show
  1. LLM.py +101 -0
  2. app.py +111 -0
  3. prompt_parser.py +35 -0
  4. requirements.txt +14 -0
  5. scoreboard.csv +11 -0
  6. scoreboard.py +28 -0
LLM.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_core.prompts import ChatPromptTemplate, PromptTemplate
2
+ from langchain_groq import ChatGroq
3
+ from langchain_huggingface import ChatHuggingFace
4
+ from langchain_huggingface import HuggingFaceEndpoint
5
+ from dotenv import load_dotenv
6
+ from langchain.schema.output_parser import StrOutputParser
7
+ from langchain_huggingface import ChatHuggingFace
8
+ from langchain_google_genai import ChatGoogleGenerativeAI
9
+ import os
10
+ from huggingface_hub import login
11
+
12
+ load_dotenv()
13
+
14
+ login(token=os.environ["HUGGING_FACE_API_KEY"])
15
+ os.environ['CURL_CA_BUNDLE'] = ''
16
+
17
+ load_dotenv()
18
+
19
+ class Bot():
20
+ def __init__(self):
21
+ self.groq_models = ['gemma-7b-it', 'llama3-70b-8192',\
22
+ 'llama3-8b-8192', 'mixtral-8x7b-32768']
23
+ self.hf_models = ["01-ai/Yi-1.5-34B-Chat", "google/gemma-1.1-2b-it",\
24
+ "google/gemma-1.1-7b-it"]
25
+ self.google_models = ["gemini-1.0-pro", "gemini-1.5-flash",\
26
+ "gemini-1.5-pro"]
27
+ self.models = ["gemini-1.0-pro", "gemini-1.5-flash", "gemini-1.5-pro", "01-ai/Yi-1.5-34B-Chat", "google/gemma-1.1-2b-it",\
28
+ "google/gemma-1.1-7b-it", 'gemma-7b-it', 'llama3-70b-8192', 'llama3-8b-8192', 'mixtral-8x7b-32768']
29
+
30
+ def call_groq(self, model, temp = 0.7, given_prompt = "Hi"):
31
+ try:
32
+ llm = ChatGroq(
33
+ temperature=temp,
34
+ model= model
35
+ )
36
+
37
+ system = "You are a helpful assistant."
38
+ human = "{text}"
39
+ prompt = ChatPromptTemplate.from_messages([("system", system), ("human", human)])
40
+
41
+ chain = prompt | llm | StrOutputParser()
42
+ return chain.invoke({"text": given_prompt})
43
+
44
+ except Exception as e:
45
+ return f"Error: {str(e)}"
46
+
47
+ def call_hf(self,model, temp = 0.7, given_prompt = "Hi"):
48
+ try:
49
+ llm = HuggingFaceEndpoint(
50
+ repo_id=model,
51
+ temperature=temp
52
+ )
53
+
54
+ chat = ChatHuggingFace(llm=llm, verbose=True)
55
+
56
+ template = """
57
+ You are a helpful assistant
58
+
59
+ User: {query}
60
+
61
+ Answer:
62
+ """
63
+
64
+ prompt = PromptTemplate(
65
+ template=template,
66
+ input_variables=["query"]
67
+ )
68
+
69
+ chain =prompt | chat | StrOutputParser()
70
+
71
+ return chain.invoke({"query": given_prompt})
72
+
73
+ except Exception as e:
74
+ return f"Error: {str(e)}"
75
+
76
+ def call_google(self,model, temp=0.7, given_prompt = "Hi"):
77
+ try:
78
+ model = ChatGoogleGenerativeAI(model = model, temprature = temp)
79
+ system = "You are a helpful assistant."
80
+ human = "{text}"
81
+ prompt = ChatPromptTemplate.from_messages([("human", human)])
82
+ chain = prompt | model | StrOutputParser()
83
+ return chain.invoke({"text": given_prompt})
84
+ except Exception as e:
85
+ return f"Error: {str(e)}"
86
+
87
+ def response(self, model, prompt="Hi", temprature = 0.7):
88
+ if model in self.groq_models:
89
+ res_show = self.call_groq(temp = temprature, given_prompt = prompt, model= model)
90
+ elif model in self.hf_models:
91
+ res_show = self.call_hf(given_prompt = prompt, temp = temprature, model = model)
92
+ elif model in self.google_models:
93
+ res_show = self.call_google(given_prompt = prompt, temp = temprature, model = model)
94
+ else:
95
+ return "Sorry! App not working properly"
96
+ return res_show
97
+
98
+
99
+
100
+
101
+
app.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from prompt_parser import Parse_Prompt
3
+ from scoreboard import Score
4
+ import warnings
5
+
6
+ warnings.filterwarnings("ignore")
7
+
8
+ arena = Parse_Prompt()
9
+ score = Score()
10
+
11
+ with gr.Blocks(fill_height = True) as app:
12
+ with gr.Tab("🪖 Battle Field"):
13
+ gr.Markdown('''## ⚔️ LLM: Large Language Mayhem
14
+ - Voting should be fair and based on the performance of the models.
15
+ - No cheating or manipulating the outcomes.
16
+ - Press 🎲 Random to change the models.
17
+ - Everything else except the Random button will only clear the screen, model being the same.
18
+ - Have fun and enjoy the language mayhem!
19
+ - Warrior names will be visible after your first query (after random also you will have to give a query to see changes)
20
+ - Don't See Warrior names before voting
21
+ ''')
22
+ with gr.Row():
23
+ with gr.Accordion("🥷 Warriors", open = False):
24
+ gr.Dataframe([[model] for model in arena.models], col_count = 1, headers = ["🥷"])
25
+ with gr.Group():
26
+ with gr.Row():
27
+ with gr.Column():
28
+ chatbox1 = gr.Chatbot(label = "Warrior A", show_copy_button = True)
29
+ with gr.Column():
30
+ chatbox2 = gr.Chatbot(label = "Warrior B", show_copy_button = True)
31
+ textbox = gr.Textbox(show_label = False, placeholder = "👉 Enter your prompt")
32
+ with gr.Row():
33
+ with gr.Accordion("🥷 Current Warriors",open = False):
34
+ with gr.Row():
35
+ war1= gr.Textbox(arena.model1, interactive= False, show_label=False)
36
+ war2 = gr.Textbox(arena.model2, interactive= False, show_label= False)
37
+ with gr.Row():
38
+ with gr.Accordion("👆 Vote", open = False):
39
+ with gr.Row():
40
+ vote_a = gr.ClearButton([textbox, chatbox1, chatbox2], value = "👈 Warrior A Wins")
41
+ vote_b = gr.ClearButton([textbox, chatbox1, chatbox2], value = "👉 Warrior B Wins")
42
+ vote_tie = gr.ClearButton([textbox, chatbox1, chatbox2], value = "🤝 Both Won")
43
+
44
+ submit_button = gr.Button("Submit")
45
+ with gr.Row():
46
+ new_round = gr.ClearButton( [textbox, chatbox1, chatbox2], value = "🎲New Round🎲")
47
+ clear = gr.ClearButton([textbox, chatbox1, chatbox2], value = "🧹 Clear")
48
+ with gr.Row():
49
+ with gr.Accordion("🔩 Parameters", open = False):
50
+ temp_slider = gr.Slider(0,1,value = 0.7, step=0.1, label = "Temprature")
51
+
52
+ textbox.submit(
53
+ fn = arena.gen_output,
54
+ inputs = [temp_slider, textbox],
55
+ outputs = [chatbox1, chatbox2]
56
+ )
57
+ textbox.submit(
58
+ fn = arena.current_model2,
59
+ outputs = war2
60
+ )
61
+ textbox.submit(
62
+ fn = arena.current_model1,
63
+ outputs = war1
64
+ )
65
+ submit_button.click(
66
+ fn = arena.gen_output,
67
+ inputs = [temp_slider, textbox],
68
+ outputs = [chatbox1, chatbox2]
69
+ )
70
+ submit_button.click(
71
+ fn = arena.current_model1,
72
+ outputs = war1
73
+ )
74
+ submit_button.click(
75
+ fn = arena.current_model2,
76
+ outputs = war2
77
+ )
78
+ vote_a.click(
79
+ fn=lambda: score.update(arena.model1, score.df)
80
+ )
81
+ vote_b.click(
82
+ fn = lambda: score.update(arena.model2, score.df)
83
+ )
84
+ vote_tie.click(
85
+ fn = arena.change_models
86
+ )
87
+ new_round.click(
88
+ fn = arena.change_models
89
+ )
90
+ clear.click(
91
+ fn = arena.clear_history
92
+ )
93
+
94
+ with gr.Tab("💯 Score Board") as data_tab:
95
+ gr.Markdown('''## ⚔️ LLM: Large Language Mayhem
96
+ - Voting should be fair and based on the performance of the models.
97
+ - No cheating or manipulating the outcomes.
98
+ - Click on Generate button to Update the 💯 Scoreboard.
99
+ ''')
100
+ gr.Interface(
101
+ fn = score.df_show,
102
+ inputs = None,
103
+ outputs=gr.Dataframe(type="pandas", label="Scoreboard", headers = ["","",""]),
104
+ live = True,
105
+ allow_flagging = "never",
106
+ clear_btn = None
107
+
108
+ )
109
+
110
+
111
+ app.launch()
prompt_parser.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from LLM import Bot
2
+ import random
3
+
4
+ class Parse_Prompt(Bot):
5
+ def __init__(self):
6
+ super().__init__()
7
+ self.change = True
8
+ self.model1 = None
9
+ self.model2 = None
10
+ self.chat_history_1 = []
11
+ self.chat_history_2 = []
12
+
13
+ def model_init(self):
14
+ return random.sample(self.models, 2)
15
+
16
+ def clear_history(self):
17
+ self.chat_history_1 = []
18
+ self.chat_history_2 = []
19
+
20
+ def change_models(self):
21
+ self.clear_history()
22
+ self.change = True
23
+
24
+ def current_model1(self):
25
+ return self.model1
26
+ def current_model2(self):
27
+ return self.model2
28
+
29
+ def gen_output(self, temp, prompt):
30
+ if self.change:
31
+ [self.model1, self.model2] = self.model_init()
32
+ self.change = False
33
+ self.chat_history_1.append([prompt, self.response(self.model1, prompt, temp)])
34
+ self.chat_history_2.append([prompt, self.response(self.model2, prompt, temp)])
35
+ return self.chat_history_1, self.chat_history_2
requirements.txt ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ langchain==0.2.6
2
+ langchain-community==0.2.6
3
+ langchain-core==0.2.10
4
+ langchain-google-genai==1.0.7
5
+ python-dotenv
6
+ langchain-groq==0.1.5
7
+ langchain-huggingface==0.0.3
8
+ langchain-text-splitters==0.2.1
9
+ requests==2.27.1
10
+ numpy
11
+ pandas
12
+ gradio
13
+ huggingface_hub
14
+ sentencepiece
scoreboard.csv ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Models,Fights Won
2
+ gemini-1.0-pro,2
3
+ gemini-1.5-flash,0
4
+ gemini-1.5-pro,1
5
+ 01-ai/Yi-1.5-34B-Chat,0
6
+ google/gemma-1.1-2b-it,5
7
+ google/gemma-1.1-7b-it,0
8
+ gemma-7b-it,0
9
+ llama3-70b-8192,0
10
+ llama3-8b-8192,2
11
+ mixtral-8x7b-32768,1
scoreboard.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ from prompt_parser import Parse_Prompt
3
+ import pandas as pd
4
+
5
+ class Score(Parse_Prompt):
6
+ def __init__(self):
7
+ super().__init__()
8
+ self.file_path = 'scoreboard.csv'
9
+ self.init_scores()
10
+
11
+ def init_scores(self):
12
+ try:
13
+ self.df = pd.read_csv(self.file_path)
14
+ except FileNotFoundError:
15
+ data = {
16
+ 'Models': self.models,
17
+ 'Fights Won': np.zeros(10, dtype = int)
18
+ }
19
+ self.df = pd.DataFrame(data)
20
+ self.df.to_csv(self.file_path, index=False)
21
+
22
+ def update(self, model, df):
23
+ df.loc[self.df["Models"] == model, 'Fights Won'] += 1
24
+ df.to_csv(self.file_path, index=False)
25
+ self.clear_history()
26
+
27
+ def df_show(self):
28
+ return pd.read_csv(self.file_path)