Logeswaransr commited on
Commit
f0235ce
·
1 Parent(s): 142fae5

Wikipedia Chat implemented

Browse files
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ wikichat
__pycache__/interaction.cpython-311.pyc ADDED
Binary file (2.32 kB). View file
 
app.py CHANGED
@@ -1,61 +1,23 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
 
3
 
4
  """
5
  For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
  """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
 
42
  """
43
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
44
  """
45
- demo = gr.ChatInterface(
46
- respond,
47
- additional_inputs=[
48
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
49
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
50
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
51
- gr.Slider(
52
- minimum=0.1,
53
- maximum=1.0,
54
- value=0.95,
55
- step=0.05,
56
- label="Top-p (nucleus sampling)",
57
- ),
58
- ],
59
  )
60
 
61
 
 
1
  import gradio as gr
2
+ from interaction import LLMResource
3
+
4
 
5
  """
6
  For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
7
  """
8
+ # client = InferenceClient(
9
+ # "HuggingFaceH4/zephyr-7b-beta",
10
+ # token=os.environ["API_KEY"]
11
+ # )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
  """
14
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
15
  """
16
+ demo = gr.Interface(
17
+ LLMResource.invoke,
18
+ title="Vicky - Wikipedia Chatbot",
19
+ inputs=[gr.Textbox(label="Input Prompt", value="When was Oppenheimer released?")],
20
+ outputs=[gr.Textbox(label="Output")]
 
 
 
 
 
 
 
 
 
21
  )
22
 
23
 
interaction.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from langchain import hub
3
+ from langchain.agents import load_tools, initialize_agent, AgentType
4
+ from langchain_core.messages import HumanMessage, SystemMessage, AIMessage
5
+ from langchain_huggingface import HuggingFaceEndpoint
6
+ from langchain_huggingface import ChatHuggingFace
7
+
8
+
9
+ class LLMResource:
10
+ llm = HuggingFaceEndpoint(
11
+ repo_id="mistralai/Mistral-7B-Instruct-v0.2",
12
+ task="text-generation",
13
+ huggingfacehub_api_token=os.environ["API_KEY"],
14
+ streaming=True,
15
+ temperature=0.5
16
+ )
17
+ chat_model = ChatHuggingFace(
18
+ llm = llm,
19
+ )
20
+ tools = load_tools(["wikipedia"], llm=llm)
21
+ agent = initialize_agent(
22
+ tools,
23
+ llm,
24
+ agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION,
25
+ handle_parsing_errors=True,
26
+ )
27
+
28
+ @staticmethod
29
+ def invoke(message:str):
30
+ response = LLMResource.agent.invoke(message)
31
+ print(response)
32
+ return response["output"][:-4]
33
+
34
+ @staticmethod
35
+ def test():
36
+ print(LLMResource.agent.invoke("When was the movie \'Oppenheimer\' released?"))
37
+
38
+ if __name__ == "__main__":
39
+ LLMResource.test()
requirements.txt CHANGED
@@ -1 +1,6 @@
1
- huggingface_hub==0.22.2
 
 
 
 
 
 
1
+ langchain
2
+ langchain-huggingface
3
+ langchain-community
4
+ huggingface-hub
5
+ gradio
6
+ wikipedia