GouthamVicky commited on
Commit
7c86aee
1 Parent(s): 478b888

Finetuned Flan Model

Browse files
Files changed (2) hide show
  1. app.py +122 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from typing import Optional, Tuple
3
+
4
+ import gradio as gr
5
+ from langchain.chains import ConversationChain
6
+ from langchain.llms import OpenAI
7
+
8
+ from langchain import PromptTemplate, HuggingFaceHub, LLMChain
9
+ from langchain.chains.conversation.memory import ConversationBufferMemory
10
+
11
+ from threading import Lock
12
+ import os
13
+
14
+
15
+ def load_chain():
16
+ """Logic for loading the chain you want to use should go here."""
17
+
18
+ template = """Question: {question}
19
+
20
+ Answer: Let's think step by step."""
21
+ prompt = PromptTemplate(template=template, input_variables=["question"])
22
+ #llm=HuggingFaceHub(repo_id="google/flan-t5-xl", model_kwargs={"temperature":1e-10})
23
+ llm=HuggingFaceHub(repo_id="google/flan-t5-xl", model_kwargs={"temperature":1e-10,"max_length":512})
24
+
25
+ chain=ConversationChain(
26
+ llm=llm,
27
+ verbose=True,
28
+ memory=ConversationBufferMemory())
29
+ return chain
30
+
31
+
32
+ def set_openai_api_key(api_key: str):
33
+ """Set the api key and return chain.
34
+
35
+ If no api_key, then None is returned.
36
+ """
37
+ if api_key:
38
+ os.environ["HUGGINGFACEHUB_API_TOKEN"] = api_key
39
+ chain = load_chain()
40
+ os.environ["HUGGINGFACEHUB_API_TOKEN"] = ""
41
+ return chain
42
+
43
+ class ChatWrapper:
44
+
45
+ def __init__(self):
46
+ self.lock = Lock()
47
+ def __call__(
48
+ self, api_key: str, inp: str, history: Optional[Tuple[str, str]], chain: Optional[ConversationChain]
49
+ ):
50
+ """Execute the chat functionality."""
51
+ self.lock.acquire()
52
+ try:
53
+ history = history or []
54
+ # If chain is None, that is because no API key was provided.
55
+ if chain is None:
56
+ history.append((inp, "Please paste API key to use ZolBot Agent"))
57
+ return history, history
58
+
59
+ # Run chain and append input.
60
+ output = chain.run(input=inp)
61
+ history.append((inp, output))
62
+ except Exception as e:
63
+ raise e
64
+ finally:
65
+ self.lock.release()
66
+ return history, history
67
+
68
+ chat = ChatWrapper()
69
+
70
+ block = gr.Blocks(css=".gradio-container {background-color: lightgray}")
71
+
72
+ with block:
73
+ with gr.Row():
74
+ gr.Markdown("<h3><center>ZolBot conversational APP</center></h3>")
75
+
76
+ openai_api_key_textbox = gr.Textbox(
77
+ placeholder="Please paste API key to use ZolBot Bot Agent",
78
+ show_label=False,
79
+ lines=1,
80
+ type="password",
81
+ )
82
+
83
+ chatbot = gr.Chatbot()
84
+
85
+ with gr.Row():
86
+ message = gr.Textbox(
87
+ label="What's your question?",
88
+ placeholder="Type your Query With prompts",
89
+ lines=1,
90
+ )
91
+ submit = gr.Button(value="Send", variant="secondary").style(full_width=False)
92
+
93
+ gr.Examples(
94
+ examples=[
95
+ "Summarize the following text: The tower is 324 metres (1,063 ft) tall, about the same height as an 81-storey building, and the tallest structure in Paris. Its base is square, measuring 125 metres (410 ft) on each side. During its construction, the Eiffel Tower surpassed the Washington Monument to become the tallest man-made structure in the world, a title it held for 41 years until the Chrysler Building in New York City was finished in 1930. It was the first structure to reach a height of 300 metres. Due to the addition of a broadcasting aerial at the top of the tower in 1957, it is now taller than the Chrysler Building by 5.2 metres (17 ft). Excluding transmitters, the Eiffel Tower is the second tallest free-standing structure in France after the Millau Viaduct.",
96
+ "Answer the following question by reasoning step by step.The cafeteria had 23 apples. If the used 20 for lunch and bougth 6 more, how many apples do they have?",
97
+ "Please answer the following question. What is the boiling point of water?",
98
+ "The tower is 324 metres (1,063 ft) tall, about the same height as an 81-storey building, and the tallest structure in Paris. Its base is square, measuring 125 metres (410 ft) on each side. During its construction, the Eiffel Tower surpassed the Washington Monument to become the tallest man-made structure in the world, a title it held for 41 years until the Chrysler Building in New York City was finished in 1930. It was the first structure to reach a height of 300 metres. Due to the addition of a broadcasting aerial at the top of the tower in 1957, it is now taller than the Chrysler Building by 5.2 metres (17 ft). Excluding transmitters, the Eiffel Tower is the second tallest free-standing structure in France after the Millau Viaduct. Q: Which material is the tower made of?"
99
+ ],
100
+ inputs=message,
101
+ )
102
+
103
+ gr.HTML("Demo application of a ZolBot conversational APP")
104
+
105
+ gr.HTML(
106
+ "<center>Powered by <a href='https://zolvit.com/'>Zolvit 🔗</a></center>"
107
+ )
108
+
109
+ state = gr.State()
110
+ agent_state = gr.State()
111
+
112
+ submit.click(chat, inputs=[openai_api_key_textbox, message, state, agent_state], outputs=[chatbot, state])
113
+ message.submit(chat, inputs=[openai_api_key_textbox, message, state, agent_state], outputs=[chatbot, state])
114
+
115
+ openai_api_key_textbox.change(
116
+ set_openai_api_key,
117
+ inputs=[openai_api_key_textbox],
118
+ outputs=[agent_state],
119
+ )
120
+
121
+
122
+ block.launch(debug=True,share=True)
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ openai
2
+ gradio
3
+ langchain