jchauhan commited on
Commit
b2e8351
1 Parent(s): c4d8524

1st version of Indic Gemma

Browse files
Files changed (3) hide show
  1. .gitignore +112 -0
  2. app.py +191 -1
  3. requirements.txt +3 -0
.gitignore ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ env/
12
+ build/
13
+ develop-eggs/
14
+ dist/
15
+ downloads/
16
+ eggs/
17
+ .eggs/
18
+ lib/
19
+ lib64/
20
+ parts/
21
+ sdist/
22
+ var/
23
+ wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+
28
+ # PyInstaller
29
+ # Usually these files are written by a python script from a template
30
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
31
+ *.manifest
32
+ *.spec
33
+
34
+ # Installer logs
35
+ pip-log.txt
36
+ pip-delete-this-directory.txt
37
+
38
+ # Unit test / coverage reports
39
+ htmlcov/
40
+ .tox/
41
+ .coverage
42
+ .coverage.*
43
+ .cache
44
+ nosetests.xml
45
+ coverage.xml
46
+ *.cover
47
+ .hypothesis/
48
+ .pytest_cache/
49
+
50
+ # Translations
51
+ *.mo
52
+ *.pot
53
+
54
+ # Django stuff:
55
+ *.log
56
+ local_settings.py
57
+
58
+ # Flask stuff:
59
+ instance/
60
+ .webassets-cache
61
+
62
+ # Scrapy stuff:
63
+ .scrapy
64
+
65
+ # Sphinx documentation
66
+ docs/_build/
67
+
68
+ # PyBuilder
69
+ target/
70
+
71
+ # Jupyter Notebook
72
+ .ipynb_checkpoints
73
+
74
+ # pyenv
75
+ .python-version
76
+
77
+ # celery beat schedule file
78
+ celerybeat-schedule
79
+
80
+ # SageMath parsed files
81
+ *.sage.py
82
+
83
+ # dotenv
84
+ .env
85
+
86
+ # virtualenv
87
+ .venv
88
+ venv/
89
+ ENV/
90
+
91
+ # Spyder project settings
92
+ .spyderproject
93
+ .spyproject
94
+
95
+ # Rope project settings
96
+ .ropeproject
97
+
98
+ # mkdocs documentation
99
+ /site
100
+
101
+ # mypy
102
+ .mypy_cache/
103
+
104
+ # IDE settings
105
+ .vscode/
106
+ .idea/
107
+
108
+
109
+ .editorconfig
110
+ .envrc
111
+
112
+ *.har
app.py CHANGED
@@ -1 +1,191 @@
1
- test
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import typing
2
+ import os
3
+ import gradio as gr
4
+ from typing import List
5
+
6
+ import urllib.parse as urlparse
7
+ import json
8
+ from gradio_client import Client as GradioClient
9
+
10
+ class GradioUserInference:
11
+ @staticmethod
12
+ def chat_interface_components(
13
+ sample_func: typing.Callable,
14
+ ):
15
+ """
16
+ The function `chat_interface_components` creates the components for a chat interface, including
17
+ a chat history, message box, buttons for submitting, stopping, and clearing the conversation,
18
+ and sliders for advanced options.
19
+ """
20
+
21
+ # _max_length = max_sequence_length
22
+ # _max_new_tokens = max_new_tokens
23
+ # _max_compile_tokens = max_compile_tokens
24
+
25
+ with gr.Column("100%"):
26
+ gr.Markdown(
27
+ f"# <h1><center style='color:#6600FF;'>[Indic Gemma 7B Demo](https://huggingface.co/Telugu-LLM-Labs/Indic-gemma-7b-finetuned-sft-Navarasa-2.0) -- Hosted on [Google Cloud TPU v4 Instance](https://cloud.google.com/tpu/docs/v4)</center></h1>",
28
+ )
29
+ history = gr.Chatbot(
30
+ elem_id="Indic",
31
+ label="Indic",
32
+ container=True,
33
+ height="55vh",
34
+ )
35
+ prompt = gr.Textbox(
36
+ show_label=False, placeholder='Enter your prompt here..', container=False
37
+ )
38
+ input = gr.Textbox(
39
+ show_label=False, placeholder='Provide Additional Input here..', container=False
40
+ )
41
+ with gr.Row():
42
+ submit = gr.Button(
43
+ value="Run",
44
+ variant="primary"
45
+ )
46
+ stop = gr.Button(
47
+ value='Stop'
48
+ )
49
+ clear = gr.Button(
50
+ value='Clear Conversation'
51
+ )
52
+ gr.Markdown(
53
+ "# <h4><center style='color:black;'>Hosted by "
54
+ "[Detoxio AI](https://detoxio.ai) for educational purpose. Thanks to Google Cloud for TPUV4 Infrastructure, and [Telugu-LLM-Labs](https://huggingface.co/Telugu-LLM-Labs) for finetuning Gemma on Indian Languages</center></h4>",
55
+ )
56
+
57
+ inputs = [
58
+ prompt,
59
+ input,
60
+ history,
61
+ ]
62
+
63
+ clear.click(fn=lambda: [], outputs=[history])
64
+ sub_event = submit.click(
65
+ fn=sample_func, inputs=inputs, outputs=[prompt, input, history]
66
+ )
67
+ txt_event = prompt.submit(
68
+ fn=sample_func, inputs=inputs, outputs=[prompt, input, history]
69
+ )
70
+ stop.click(
71
+ fn=None,
72
+ inputs=None,
73
+ outputs=None,
74
+ cancels=[txt_event, sub_event]
75
+ )
76
+
77
+ def _handle_gradio_input(
78
+ self,
79
+ prompt: str,
80
+ input: str,
81
+ history: List[List[str]],
82
+ ):
83
+ raise NotImplementedError()
84
+
85
+ def build_inference(
86
+ self,
87
+ sample_func: typing.Callable,
88
+ ) -> gr.Blocks:
89
+ """
90
+ The function "build_inference" returns a gr.Blocks object that model
91
+ interface components.
92
+ :return: a gr.Blocks object.
93
+ """
94
+ with gr.Blocks() as block:
95
+ self.chat_interface_components(
96
+ sample_func=sample_func,
97
+ )
98
+ return block
99
+
100
+
101
+ class AssistantRole:
102
+ def __init__(self, name, seed_urls, poison_files_pattern):
103
+ self.name = name
104
+ self.seed_urls = seed_urls
105
+ self.poison_files_pattern = poison_files_pattern
106
+
107
+
108
+
109
+ class OutputParsingException(Exception):
110
+ pass
111
+
112
+ class RemoteLLM(object):
113
+ def __init__(self, base_url):
114
+ """
115
+ Initialize instance.
116
+
117
+ Parameters:
118
+ - base_url (str): Base URL of the API.
119
+ """
120
+ self._base_url = base_url
121
+ self._client = GradioClient(base_url, verbose=False)
122
+
123
+ def generate(self, prompt: str):
124
+ """
125
+ Generate text using the model.
126
+
127
+ Parameters:
128
+ - prompt (str): Input prompt to be generated.
129
+
130
+ Returns:
131
+ - str: Reponse
132
+
133
+ """
134
+ result = self._client.predict(
135
+ prompt, # str in 'parameter_24' Textbox component
136
+ [], # Tuple[str | Dict(file: filepath, alt_text: str | None) | None, str | Dict(file: filepath, alt_text: str | None) | None] in Chatbot component
137
+ "", # str in 'System Prompt' Textbox component
138
+ "Chat", # Literal['Chat', 'Instruct'] in 'Mode' Dropdown component
139
+ 2048, # float (numeric value between 1 and 10000) in 'Max Tokens' Slider component
140
+ 360, # float (numeric value between 256 and 10000) in 'Max New Tokens' Slider component
141
+ 256, # float (numeric value between 256 and 256) in 'Max Compile Tokens' Slider component
142
+ True, # Literal[] in 'Do Sample or Greedy Generation' Radio component
143
+ 1, # float (numeric value between 0.1 and 1) in 'Temperature' Slider component
144
+ 1, # float (numeric value between 0.1 and 1) in 'Top P' Slider component
145
+ 50, # float (numeric value between 1 and 100) in 'Top K' Slider component
146
+ 5, # float (numeric value between 0.1 and 5) in 'Repetition Penalty' Slider component
147
+ api_name="/sample_gradio"
148
+ )
149
+
150
+ try:
151
+ out = result[1][0][1]
152
+ return out
153
+ except Exception as ex:
154
+ raise OutputParsingException(ex, result)
155
+
156
+
157
+ class RAGApp(GradioUserInference):
158
+ def __init__(self, url):
159
+ self._llm = RemoteLLM(url)
160
+ self._gradio_app_handle=None
161
+
162
+ def _generate(self, prompt, input):
163
+ if input:
164
+ combined_prompt = f"{prompt} INPUT {input}"
165
+ else:
166
+ combined_prompt = prompt
167
+ return self._llm.generate(combined_prompt)
168
+
169
+ def _handle_gradio_input(self,
170
+ prompt: str,
171
+ input: str,
172
+ history: List[List[str]]):
173
+ response = self._generate(prompt, input)
174
+ if input:
175
+ combined_prompt = f"{prompt}\n\n\"{input}\""
176
+ else:
177
+ combined_prompt = prompt
178
+ history.append([combined_prompt, ""])
179
+ history[-1][-1] = response
180
+ yield "", "", history
181
+ # Initial update of documents and launch interface
182
+
183
+ def run(self):
184
+ self._gradio_app_handle = self.build_inference(self._handle_gradio_input)
185
+ self._gradio_app_handle.launch()
186
+
187
+
188
+ if __name__ == "__main__":
189
+ url = os.environ['INDIC_GEMMA_HOSTED_URL']
190
+ app = RAGApp(url)
191
+ app.run()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ retry
2
+ gradio-client
3
+ gradio