alpcansoydas commited on
Commit
d1e081c
·
verified ·
1 Parent(s): e670dc9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -0
app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from langchain.prompts import PromptTemplate
3
+ from langchain_huggingface import HuggingFaceEndpoint
4
+ from langchain_core.output_parsers import JsonOutputParser
5
+ from langdetect import detect
6
+ import time
7
+
8
+ # Initialize the LLM and other components
9
+ llm = HuggingFaceEndpoint(
10
+ repo_id="mistralai/Mistral-7B-Instruct-v0.3",
11
+ task="text-generation",
12
+ max_new_tokens=128,
13
+ temperature=0.7,
14
+ do_sample=False,
15
+ )
16
+
17
+ template_classify = '''
18
+ You are a topic detector bot. Your task is to determine the main topic of given text phrase.
19
+ Answer general main topic not specific words.
20
+ Your answer does not contain specific information from given text.
21
+ Answer just one general main topic. Do not answer two or more topic.
22
+ Answer shortly with two or three word phrase. Do not answer with long sentence.
23
+ Answer topic with context. Example, if it says "My delivery is late", its topic is late delivery.
24
+ If you do not know the topic just answer as General.
25
+ What is the main topic of given text?:
26
+ <text>
27
+ {TEXT}
28
+ </text>
29
+ convert it to json format using 'Answer' as key and return it.
30
+ Your final response MUST contain only the response, no other text.
31
+ Example:
32
+ {{"Answer":["General"]}}
33
+ '''
34
+
35
+ json_output_parser = JsonOutputParser()
36
+
37
+ # Define the classify_text function
38
+ def classify_text(text):
39
+ global llm
40
+
41
+ start = time.time()
42
+ try:
43
+ lang = detect(text)
44
+
45
+ except:
46
+ lang = "en"
47
+
48
+ prompt_classify = PromptTemplate(
49
+ template=template_classify,
50
+ input_variables=["LANG", "TEXT"]
51
+ )
52
+ formatted_prompt = prompt_classify.format(TEXT=text, LANG=lang)
53
+ classify = llm.invoke(formatted_prompt)
54
+
55
+ parsed_output = json_output_parser.parse(classify)
56
+ end = time.time()
57
+ duration = end - start
58
+ return lang, parsed_output["Answer"][0], duration #['Answer']
59
+
60
+ # Create the Gradio interface
61
+ def create_gradio_interface():
62
+ with gr.Blocks() as iface:
63
+ text_input = gr.Textbox(label="Text")
64
+ lang_output = gr.Textbox(label="Detected Language")
65
+ output_text = gr.Textbox(label="Detected Topics")
66
+ time_taken = gr.Textbox(label="Time Taken (seconds)")
67
+ submit_btn = gr.Button("Detect topic")
68
+
69
+ def on_submit(text):
70
+ lang, classification, duration = classify_text(text)
71
+ return lang, classification, f"Time taken: {duration:.2f} seconds"
72
+
73
+ submit_btn.click(fn=on_submit, inputs=text_input, outputs=[lang_output, output_text, time_taken])
74
+
75
+ iface.launch()
76
+
77
+ if __name__ == "__main__":
78
+ create_gradio_interface()