iSathyam03 commited on
Commit
d3887bd
β€’
1 Parent(s): db48dc9

application

Browse files
Files changed (1) hide show
  1. app.py +138 -0
app.py ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from langchain_core.output_parsers import StrOutputParser
3
+ from langchain_core.prompts import PromptTemplate
4
+ from langchain_community.llms import Ollama
5
+ import os
6
+ from dotenv import load_dotenv
7
+
8
+ # Load environment variables
9
+ load_dotenv()
10
+
11
+ # Langsmith Tracking
12
+ os.environ["LANGCHAIN_API_KEY"] = os.getenv("LANGCHAIN_API_KEY")
13
+ os.environ["LANGCHAIN_TRACING_V2"] = "true"
14
+ os.environ["LANGCHAIN_PROJECT"] = "Enhanced Q&A Chatbot With Ollama"
15
+
16
+ # Prompt template
17
+ template = """
18
+ Below is a draft text that may be poorly worded.
19
+ Your goal is to:
20
+ - Properly redact the draft text
21
+ - Convert the draft text to a specified tone (if selected)
22
+ - Convert the draft text to a specified dialect (if selected)
23
+
24
+ Here are some examples of different tones:
25
+ - Formal: Greetings! OpenAI has announced that Sam Altman is rejoining the company as its Chief Executive Officer. After a period of five days of conversations, discussions, and deliberations, the decision to bring back Altman, who had been previously dismissed, has been made. We are delighted to welcome Sam back to OpenAI.
26
+ - Informal: Hey everyone, it's been a wild week! We've got some exciting news to share - Sam Altman is back at OpenAI, taking up the role of chief executive. After a bunch of intense talks, debates, and convincing, Altman is making his triumphant return to the AI startup he co-founded.
27
+
28
+ Here are some examples of words in different dialects:
29
+ - American: French Fries, cotton candy, apartment, garbage, \
30
+ cookie, green thumb, parking lot, pants, windshield
31
+ - British: chips, candyfloss, flag, rubbish, biscuit, green fingers, \
32
+ car park, trousers, windscreen
33
+
34
+ Example Sentences from each dialect:
35
+ - American: Greetings! OpenAI has announced that Sam Altman is rejoining the company as its Chief Executive Officer. After a period of five days of conversations, discussions, and deliberations, the decision to bring back Altman, who had been previously dismissed, has been made. We are delighted to welcome Sam back to OpenAI.
36
+ - British: On Wednesday, OpenAI, the esteemed artificial intelligence start-up, announced that Sam Altman would be returning as its Chief Executive Officer. This decisive move follows five days of deliberation, discourse and persuasion, after Altman's abrupt departure from the company which he had co-established.
37
+
38
+ Please start the redaction with a warm introduction. Add the introduction \
39
+ if you need to.
40
+
41
+ Below is the draft text, tone, and dialect:
42
+ DRAFT: {draft}
43
+ TONE: {tone}
44
+ DIALECT: {dialect}
45
+
46
+ YOUR RESPONSE:
47
+ """
48
+
49
+ # PromptTemplate variables definition
50
+ prompt = PromptTemplate(
51
+ input_variables=["tone", "dialect", "draft"],
52
+ template=template,
53
+ )
54
+
55
+ def generate_response(input_data, llm):
56
+ """
57
+ Generate the rewritten response based on the input data.
58
+ """
59
+ output_parser = StrOutputParser()
60
+ chain = prompt | llm | output_parser
61
+ answer = chain.invoke(input_data)
62
+ return answer
63
+
64
+ # Streamlit App Configuration
65
+ st.set_page_config(
66
+ page_title="Rewrite Your Text 🎨",
67
+ page_icon="πŸ“",
68
+ layout="centered",
69
+ )
70
+
71
+ # Header Section
72
+ st.title("πŸ“ Rewrite Your Text with Style")
73
+ st.subheader("✨ Powered by LangChain and Ollama ✨")
74
+ st.markdown(
75
+ """
76
+ Welcome to the **Text Rewrite Assistant**! πŸŽ‰
77
+ Transform your draft text into a masterpiece by applying a custom tone and dialect.
78
+ Simply input your text, select your preferences, and let AI do the rest! πŸš€
79
+ """
80
+ )
81
+
82
+ # Input Section
83
+ st.markdown("## πŸ“‹ Enter Your Text")
84
+ def get_draft():
85
+ """
86
+ Get the draft text from the user input.
87
+ """
88
+ draft_text = st.text_area(label="Your Text Here(Max 1500 words)✍️", placeholder="Enter your draft text...", key="draft_input")
89
+ return draft_text
90
+
91
+ draft_input = get_draft()
92
+
93
+ if len(draft_input.split(" ")) > 1500:
94
+ st.error("🚫 Your text is too long! Please keep it under 700 words.")
95
+ st.stop()
96
+
97
+ # Tone and Dialect Options
98
+ st.markdown("## 🎭 Customize Your Rewrite")
99
+ col1, col2 = st.columns(2)
100
+ with col1:
101
+ option_tone = st.checkbox('Apply a Tone? 🎡', value=True)
102
+ selected_tone = st.selectbox(
103
+ 'Select a Tone:',
104
+ ('Formal', 'Informal'),
105
+ disabled=not option_tone,
106
+ )
107
+
108
+ with col2:
109
+ option_dialect = st.checkbox('Apply a Dialect? 🌍', value=True)
110
+ selected_dialect = st.selectbox(
111
+ 'Select a Dialect:',
112
+ ('American', 'British'),
113
+ disabled=not option_dialect,
114
+ )
115
+
116
+ # Generate Button
117
+ st.markdown("---")
118
+ if st.button("✨ Rewrite My Text ✨"):
119
+ # Initialize the LLM
120
+ llm = Ollama(model="llama3.1")
121
+
122
+ # Build input data
123
+ input_data = {
124
+ "draft": draft_input,
125
+ "tone": selected_tone if option_tone else "No tone specified",
126
+ "dialect": selected_dialect if option_dialect else "No dialect specified",
127
+ }
128
+
129
+ # Generate the response
130
+ try:
131
+ st.spinner("Generating your rewritten text... πŸ› οΈ")
132
+ answer = generate_response(input_data, llm)
133
+
134
+ # Display the result
135
+ st.markdown("### 🌟 Your Rewritten Text 🌟")
136
+ st.success(answer)
137
+ except Exception as e:
138
+ st.error(f"❌ An error occurred: {str(e)}")