Spaces:
Running
Running
updated using langchain and openai
Browse files- app.py +42 -0
- requirements.txt +4 -1
app.py
CHANGED
@@ -5,6 +5,13 @@ from bs4 import BeautifulSoup
|
|
5 |
import PyPDF2
|
6 |
import docx
|
7 |
import time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
10 |
sentiment_analyzer = pipeline("sentiment-analysis")
|
@@ -66,6 +73,24 @@ def analyze_text(input_text, input_type, tasks, progress=gr.Progress()):
|
|
66 |
|
67 |
return original_text, summary, sentiment, ", ".join(topics)
|
68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
def create_interface():
|
70 |
with gr.Blocks(title="Text Analysis App") as interface:
|
71 |
input_type = gr.Dropdown(["Text", "URL", "File"], label="Input Type")
|
@@ -86,6 +111,11 @@ def create_interface():
|
|
86 |
sentiment_output = gr.Textbox(label="Sentiment")
|
87 |
with gr.Tab("Topics"):
|
88 |
topics_output = gr.Textbox(label="Topics")
|
|
|
|
|
|
|
|
|
|
|
89 |
|
90 |
def update_input_visibility(input_type):
|
91 |
text_input.visible = input_type == "Text"
|
@@ -116,6 +146,18 @@ def create_interface():
|
|
116 |
outputs=[original_text_output, summary_output, sentiment_output, topics_output]
|
117 |
)
|
118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
return interface
|
120 |
|
121 |
if __name__ == "__main__":
|
|
|
5 |
import PyPDF2
|
6 |
import docx
|
7 |
import time
|
8 |
+
from langchain import OpenAI, ConversationChain, PromptTemplate
|
9 |
+
from dotenv import load_dotenv
|
10 |
+
import os
|
11 |
+
|
12 |
+
load_dotenv() # Load environment variables from .env file
|
13 |
+
openai_api_key = os.getenv("openai_api_key")
|
14 |
+
llm = OpenAI(openai_api_key=openai_api_key)
|
15 |
|
16 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
17 |
sentiment_analyzer = pipeline("sentiment-analysis")
|
|
|
73 |
|
74 |
return original_text, summary, sentiment, ", ".join(topics)
|
75 |
|
76 |
+
def chat(input_text, chat_history):
|
77 |
+
prompt_template = """
|
78 |
+
Assistant is an AI language model that helps with text analysis tasks.
|
79 |
+
|
80 |
+
{chat_history}
|
81 |
+
Human: {input_text}
|
82 |
+
Assistant:"""
|
83 |
+
|
84 |
+
prompt = PromptTemplate(
|
85 |
+
input_variables=["chat_history", "input_text"],
|
86 |
+
template=prompt_template
|
87 |
+
)
|
88 |
+
|
89 |
+
chain = ConversationChain(llm=llm, prompt=prompt)
|
90 |
+
response = chain.predict(input_text=input_text)
|
91 |
+
|
92 |
+
return response
|
93 |
+
|
94 |
def create_interface():
|
95 |
with gr.Blocks(title="Text Analysis App") as interface:
|
96 |
input_type = gr.Dropdown(["Text", "URL", "File"], label="Input Type")
|
|
|
111 |
sentiment_output = gr.Textbox(label="Sentiment")
|
112 |
with gr.Tab("Topics"):
|
113 |
topics_output = gr.Textbox(label="Topics")
|
114 |
+
with gr.Tab("Conversation"):
|
115 |
+
conversation_history = gr.State([])
|
116 |
+
conversation_input = gr.Textbox(label="Human")
|
117 |
+
conversation_output = gr.Textbox(label="Assistant")
|
118 |
+
conversation_button = gr.Button("Send")
|
119 |
|
120 |
def update_input_visibility(input_type):
|
121 |
text_input.visible = input_type == "Text"
|
|
|
146 |
outputs=[original_text_output, summary_output, sentiment_output, topics_output]
|
147 |
)
|
148 |
|
149 |
+
def process_conversation(conversation_history, conversation_input):
|
150 |
+
conversation_history.append(f"Human: {conversation_input}")
|
151 |
+
response = chat(conversation_input, "\n".join(conversation_history))
|
152 |
+
conversation_history.append(f"Assistant: {response}")
|
153 |
+
return conversation_history, "", response
|
154 |
+
|
155 |
+
conversation_button.click(
|
156 |
+
fn=process_conversation,
|
157 |
+
inputs=[conversation_history, conversation_input],
|
158 |
+
outputs=[conversation_history, conversation_input, conversation_output]
|
159 |
+
)
|
160 |
+
|
161 |
return interface
|
162 |
|
163 |
if __name__ == "__main__":
|
requirements.txt
CHANGED
@@ -4,4 +4,7 @@ torch
|
|
4 |
requests
|
5 |
beautifulsoup4
|
6 |
PyPDF2
|
7 |
-
python-docx
|
|
|
|
|
|
|
|
4 |
requests
|
5 |
beautifulsoup4
|
6 |
PyPDF2
|
7 |
+
python-docx
|
8 |
+
langchain
|
9 |
+
openai
|
10 |
+
python-dotenv
|