ahmeds26 commited on
Commit
4a6aa55
·
1 Parent(s): a63edec

Add feature chatgpt and bard

Browse files
Files changed (5) hide show
  1. app.py +57 -17
  2. bard_model.py +10 -0
  3. llm_model.py +9 -0
  4. openai_model.py +15 -0
  5. requirements.txt +2 -0
app.py CHANGED
@@ -2,6 +2,9 @@ import gradio as gr
2
  from transformers import pipeline
3
  from transformers import T5Tokenizer, T5ForConditionalGeneration
4
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
 
 
 
5
  import tensorflow
6
  import torch
7
  import random
@@ -13,42 +16,79 @@ global default_model_name
13
  default_model_name = "google/flan-t5-base"
14
 
15
 
16
- def predict(input_text, model_name):
17
- if model_name == "":
18
- model_name = default_model_name
 
 
 
 
 
 
 
 
 
 
19
 
20
- pipe = pipeline("text2text-generation", model=model_name)
21
- generated_text = pipe(input_text, max_new_tokens=1000)
22
 
23
- return generated_text[0]['generated_text']
24
 
 
 
 
 
 
 
 
25
 
26
- with gr.Blocks() as demo:
 
 
 
 
 
27
  gr.Markdown(
28
  """
29
  # Chatbot to interact with different Large Language Models (LLMs)
30
- [Here](https://huggingface.co/models?pipeline_tag=text2text-generation) are some popular text2text large lamguage models.
31
- Or use default model **"google/flan-t5-base"**
 
 
32
  """)
33
- input_model = gr.Textbox(label="Enter a custom Large Language Model name (LLM):")
34
- chatbot = gr.Chatbot(height=300, label="A chatbot to interact with llm", avatar_images=((os.path.join(os.path.dirname(__file__), "user.png")), (os.path.join(os.path.dirname(__file__), "bot.png"))))
35
- user_input = gr.Textbox()
36
- clear = gr.ClearButton([user_input, chatbot, input_model])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
  def user(user_message, chat_history):
39
  return "", chat_history + [[user_message, None]]
40
 
41
- def respond(chat_history, input_model):
42
- bot_message = predict(chat_history[-1][0], input_model)
43
  chat_history[-1][1] = bot_message
44
  time.sleep(2)
45
  return chat_history
46
 
47
  user_input.submit(user, [user_input, chatbot], [user_input, chatbot], queue=False).then(
48
- respond, [chatbot, input_model], chatbot
49
  )
50
 
51
- clear.click(lambda: None, None, chatbot, queue=False)
52
 
53
  demo.queue()
54
  demo.launch()
 
2
  from transformers import pipeline
3
  from transformers import T5Tokenizer, T5ForConditionalGeneration
4
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
5
+ from openai_model import *
6
+ from bard_model import *
7
+ from llm_model import *
8
  import tensorflow
9
  import torch
10
  import random
 
16
  default_model_name = "google/flan-t5-base"
17
 
18
 
19
+ def predict(input_text, input_prompt, input_model1, input_model2, input_model3):
20
+ if input_model1!="":
21
+ model_name = input_model1
22
+ response_text = llm_predict(input_text, model_name)
23
+ return response_text
24
+ elif input_model2!="":
25
+ model_name = input_model2
26
+ response_text = openai_predict(input_text, input_prompt, model_name)
27
+ return response_text
28
+ else:
29
+ model_name = input_model3
30
+ response_text = bard_predict(input_text, model_name)
31
+ return response_text
32
 
 
 
33
 
 
34
 
35
+ def select_choice(choice):
36
+ if choice == "Custom Large Language Model (LLM)":
37
+ return [gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)]
38
+ elif choice == "OpenAI 'GPT-3.5-Turbo' Model":
39
+ return [gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)]
40
+ else:
41
+ return [gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)]
42
 
43
+ input_model1 = gr.Textbox(label="Enter LLM name:")
44
+ input_model2 = gr.Textbox(label="Enter OpenAI API KEY:")
45
+ input_model3 = gr.Textbox(label="Enter Google Bard API KEY:")
46
+
47
+
48
+ with gr.Blocks(theme=gr.themes.Glass(primary_hue="blue", secondary_hue="sky", neutral_hue="stone")) as demo:
49
  gr.Markdown(
50
  """
51
  # Chatbot to interact with different Large Language Models (LLMs)
52
+ [Here](https://huggingface.co/models?pipeline_tag=text2text-generation) are some popular custom text2text large lamguage models.
53
+ For example: **"google/flan-t5-base"**
54
+ OR Your **OPEN AI API KEY** to chat with **ChatGPT**
55
+ OR Your **Google Bard API KEY** to chat with **Google Bard**
56
  """)
57
+ input_models = gr.Radio(choices=["Custom Large Language Model (LLM)", "OpenAI 'GPT-3.5-Turbo' Model",
58
+ "Google Bard Model"], label="Please Select which model you want to use:",
59
+ value="Custom Large Language Model (LLM):")
60
+
61
+ with gr.Row(visible=True) as row:
62
+ with gr.Column(visible=True) as col:
63
+ with gr.Row(visible=True) as rowA:
64
+ input_model1.render()
65
+ with gr.Row(visible=False) as rowB:
66
+ input_model2.render()
67
+ with gr.Row(visible=False) as rowC:
68
+ input_model3.render()
69
+
70
+
71
+ chatbot = gr.Chatbot(height=300, label="A chatbot to interact with LLM", avatar_images=((os.path.join(os.path.dirname(__file__), "user.png")), (os.path.join(os.path.dirname(__file__), "bot.png"))))
72
+ user_input = gr.Textbox(label="Enter a message:")
73
+ clear = gr.ClearButton(value="Clear")
74
+ custom_prompt = gr.Textbox(label="Enter a custom prompt to use:", lines=15, max_lines=20, placeholder="In just 150 words summarize this text...")
75
+
76
+ input_models.change(fn=select_choice, inputs=input_models, outputs=[rowA, rowB, rowC], queue=False)
77
 
78
  def user(user_message, chat_history):
79
  return "", chat_history + [[user_message, None]]
80
 
81
+ def respond(chat_history, custom_prompt, input_model1, input_model2, input_model3):
82
+ bot_message = predict(chat_history[-1][0], custom_prompt, input_model1, input_model2, input_model3)
83
  chat_history[-1][1] = bot_message
84
  time.sleep(2)
85
  return chat_history
86
 
87
  user_input.submit(user, [user_input, chatbot], [user_input, chatbot], queue=False).then(
88
+ respond, [chatbot, custom_prompt, input_model1, input_model2, input_model3], chatbot
89
  )
90
 
91
+ clear.click(lambda: [None, None, None, None, None, None], outputs=[user_input, chatbot, custom_prompt, input_model1, input_model2, input_model3], queue=False)
92
 
93
  demo.queue()
94
  demo.launch()
bard_model.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ from bardapi import Bard
2
+
3
+
4
+
5
+ def bard_predict(input_text, bard_api_key):
6
+
7
+ bard = Bard(api_key=bard_api_key)
8
+ response = bard.get_answer(input_text)
9
+
10
+ return response['content']
llm_model.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
2
+ import tensorflow
3
+ import torch
4
+
5
+
6
+ def llm_predict(input_text, model_name):
7
+ pipe = pipeline("text2text-generation", model=model_name)
8
+ generated_text = pipe(input_text, max_new_tokens=1000)
9
+ return generated_text[0]['generated_text']
openai_model.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import openai
2
+ import sys
3
+ import os
4
+
5
+
6
+ def openai_predict(message, input_prompt, openai_api_key):
7
+ openai.api_key = openai_api_key
8
+
9
+ response = openai.ChatCompletion.create(
10
+ model='gpt-3.5-turbo',
11
+ messages=[{"role": "user", "content": f"{input_prompt}: {message}"}],
12
+ temperature=1.0
13
+ )
14
+
15
+ return response["choices"][0]["message"]["content"]
requirements.txt CHANGED
@@ -1,4 +1,6 @@
 
1
  gradio
 
2
  tensorflow
3
  tensorflow_intel
4
  torch
 
1
+ bardapi
2
  gradio
3
+ openai
4
  tensorflow
5
  tensorflow_intel
6
  torch