ad4r5hgs commited on
Commit
6a8925d
1 Parent(s): 2cbfea1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -0
app.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from dotenv import load_dotenv
3
+ import gradio as gr
4
+ from langchain_google_genai.chat_models import ChatGoogleGenerativeAI
5
+ from langchain_cohere import ChatCohere
6
+ from langchain_core.prompts import ChatPromptTemplate
7
+ from langchain.schema.output_parser import StrOutputParser
8
+
9
+ load_dotenv()
10
+
11
+ COHERE_API_KEY = os.getenv('COHERE_API_KEY')
12
+ GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY')
13
+
14
+ #! 1) LOAD THE MODEL
15
+ def load_model(model: str, temp: float, max_tokens: int):
16
+ if model == "Cohere Command":
17
+ return ChatCohere(
18
+ cohere_api_key=COHERE_API_KEY,
19
+ max_tokens=max_tokens,
20
+ temperature=temp
21
+ )
22
+ else:
23
+ return ChatGoogleGenerativeAI(
24
+ model="gemini-pro",
25
+ google_api_key=GOOGLE_API_KEY,
26
+ max_tokens=max_tokens,
27
+ temperature=temp
28
+ )
29
+
30
+ #! 2) CONSTRUCT THE PROMPT
31
+ template = """
32
+ You are a sentiment analysis AI tool, you should accurately analyse given description and predict the underlying sentiment
33
+
34
+ Human: Following is the description: {description}
35
+ """
36
+
37
+ prompt_template = ChatPromptTemplate.from_template(template)
38
+
39
+ def submit(model, temperature, max_tokens, input_text):
40
+ llm = load_model(model, temperature, max_tokens)
41
+
42
+ #! 3) SETUP THE CHAIN
43
+ chain = prompt_template | llm | StrOutputParser()
44
+
45
+ #! 4) INVOKE THE CHAIN
46
+ result = chain.invoke(
47
+ {
48
+ "description": input_text
49
+ }
50
+ )
51
+ return result
52
+
53
+ def demo():
54
+ with gr.Blocks() as demo:
55
+ with gr.Tabs() as tabs:
56
+
57
+ with gr.Tab("Model Selection"):
58
+ with gr.Row():
59
+ model = gr.Dropdown(choices=["Cohere Command", "Gemini Pro"], label="Select Model")
60
+ with gr.Row():
61
+ temperature = gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.1, label="Temperature")
62
+ with gr.Row():
63
+ max_tokens = gr.Slider(minimum=50, maximum=2048, value=150, step=10, label="Max Tokens")
64
+
65
+ with gr.Tab("Sentiment Analysis"):
66
+ with gr.Row():
67
+ input_text = gr.Textbox(lines=5, placeholder="Enter your text here...", label="Input")
68
+ with gr.Row():
69
+ output_text = gr.Textbox(lines=5, placeholder="Output will appear here...", label="Output")
70
+ with gr.Row():
71
+ submit_button = gr.Button("Submit")
72
+ submit_button.click(submit, inputs=[model, temperature, max_tokens, input_text], outputs=output_text)
73
+
74
+ demo.launch(share=True)
75
+
76
+ if __name__ == "__main__":
77
+ demo()