Spaces:
Sleeping
Sleeping
Update app.py
#1
by
Chan-Y
- opened
app.py
CHANGED
|
@@ -35,19 +35,6 @@ Example:
|
|
| 35 |
{{"Answer":["General"]}}
|
| 36 |
'''
|
| 37 |
|
| 38 |
-
"""
|
| 39 |
-
template_json = '''
|
| 40 |
-
Your task is to read the following text, convert it to json format using 'Answer' as key and return it.
|
| 41 |
-
<text>
|
| 42 |
-
{RESPONSE}
|
| 43 |
-
</text>
|
| 44 |
-
|
| 45 |
-
Your final response MUST contain only the response, no other text.
|
| 46 |
-
Example:
|
| 47 |
-
{{"Answer":["General"]}}
|
| 48 |
-
'''
|
| 49 |
-
"""
|
| 50 |
-
|
| 51 |
json_output_parser = JsonOutputParser()
|
| 52 |
|
| 53 |
# Define the classify_text function
|
|
@@ -55,16 +42,9 @@ def classify_text(text):
|
|
| 55 |
global llm
|
| 56 |
|
| 57 |
start = time.time()
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
"en": "english",
|
| 62 |
-
"ar": "arabic",
|
| 63 |
-
"es": "spanish",
|
| 64 |
-
"it": "italian",
|
| 65 |
-
}
|
| 66 |
-
try:
|
| 67 |
-
lang = language_map[lang]
|
| 68 |
except:
|
| 69 |
lang = "en"
|
| 70 |
|
|
@@ -75,36 +55,27 @@ def classify_text(text):
|
|
| 75 |
formatted_prompt = prompt_classify.format(TEXT=text, LANG=lang)
|
| 76 |
classify = llm.invoke(formatted_prompt)
|
| 77 |
|
| 78 |
-
'''
|
| 79 |
-
prompt_json = PromptTemplate(
|
| 80 |
-
template=template_json,
|
| 81 |
-
input_variables=["RESPONSE"]
|
| 82 |
-
)
|
| 83 |
-
'''
|
| 84 |
-
|
| 85 |
-
#formatted_prompt = template_json.format(RESPONSE=classify)
|
| 86 |
-
#response = llm.invoke(formatted_prompt)
|
| 87 |
-
|
| 88 |
parsed_output = json_output_parser.parse(classify)
|
| 89 |
end = time.time()
|
| 90 |
duration = end - start
|
| 91 |
-
return parsed_output, duration #['Answer']
|
| 92 |
|
| 93 |
# Create the Gradio interface
|
| 94 |
-
def gradio_app(text):
|
| 95 |
-
classification, time_taken = classify_text(text)
|
| 96 |
-
return classification, f"Time taken: {time_taken:.2f} seconds"
|
| 97 |
-
|
| 98 |
def create_gradio_interface():
|
| 99 |
with gr.Blocks() as iface:
|
| 100 |
text_input = gr.Textbox(label="Text")
|
|
|
|
| 101 |
output_text = gr.Textbox(label="Detected Topics")
|
| 102 |
time_taken = gr.Textbox(label="Time Taken (seconds)")
|
| 103 |
submit_btn = gr.Button("Detect topic")
|
| 104 |
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
iface.launch()
|
| 108 |
|
| 109 |
if __name__ == "__main__":
|
| 110 |
-
create_gradio_interface()
|
|
|
|
| 35 |
{{"Answer":["General"]}}
|
| 36 |
'''
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
json_output_parser = JsonOutputParser()
|
| 39 |
|
| 40 |
# Define the classify_text function
|
|
|
|
| 42 |
global llm
|
| 43 |
|
| 44 |
start = time.time()
|
| 45 |
+
try:
|
| 46 |
+
lang = detect(text)
|
| 47 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
except:
|
| 49 |
lang = "en"
|
| 50 |
|
|
|
|
| 55 |
formatted_prompt = prompt_classify.format(TEXT=text, LANG=lang)
|
| 56 |
classify = llm.invoke(formatted_prompt)
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
parsed_output = json_output_parser.parse(classify)
|
| 59 |
end = time.time()
|
| 60 |
duration = end - start
|
| 61 |
+
return lang, parsed_output["Answer"][0], duration #['Answer']
|
| 62 |
|
| 63 |
# Create the Gradio interface
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
def create_gradio_interface():
|
| 65 |
with gr.Blocks() as iface:
|
| 66 |
text_input = gr.Textbox(label="Text")
|
| 67 |
+
lang_output = gr.Textbox(label="Detected Language")
|
| 68 |
output_text = gr.Textbox(label="Detected Topics")
|
| 69 |
time_taken = gr.Textbox(label="Time Taken (seconds)")
|
| 70 |
submit_btn = gr.Button("Detect topic")
|
| 71 |
|
| 72 |
+
def on_submit(text):
|
| 73 |
+
lang, classification, duration = classify_text(text)
|
| 74 |
+
return lang, classification, f"Time taken: {duration:.2f} seconds"
|
| 75 |
+
|
| 76 |
+
submit_btn.click(fn=on_submit, inputs=text_input, outputs=[lang_output, output_text, time_taken])
|
| 77 |
|
| 78 |
iface.launch()
|
| 79 |
|
| 80 |
if __name__ == "__main__":
|
| 81 |
+
create_gradio_interface()
|