Skip to content

Instantly share code, notes, and snippets.

@SharathHebbar
Created September 27, 2024 08:46
Show Gist options
  • Select an option

  • Save SharathHebbar/44e0a7da3e52a8fe55662bb40c6fe647 to your computer and use it in GitHub Desktop.

Select an option

Save SharathHebbar/44e0a7da3e52a8fe55662bb40c6fe647 to your computer and use it in GitHub Desktop.
Main
from flask import Flask, render_template, request, redirect, url_for
from langchain import LLMChain
from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate
import spacy
app = Flask(__name__)
# Load spaCy's English model for entity recognition
nlp = spacy.load("en_core_web_sm")
# GPT-4 setup
llm = OpenAI(temperature=0.7, model="gpt-4")
# Chat prompt template
chat_prompt = PromptTemplate(
input_variables=["text"],
template="You are a helpful assistant. {text}"
)
# Function to extract entities and generate highlighted text
def extract_entities_and_highlight(text):
doc = nlp(text)
highlighted_text = text
for ent in doc.ents:
if ent.label_ in ["PERSON", "ORG", "GPE", "LOC", "MEDICAL", "DRUG", "DISEASE"]:
highlighted_text = highlighted_text.replace(ent.text, f'<span class="entity">{ent.text}</span>')
return highlighted_text
# Route for rendering the chat page
@app.route('/', methods=['GET', 'POST'])
def chat():
conversation = []
if request.method == 'POST':
user_input = request.form['message']
# Generate a response from GPT-4
chain = LLMChain(llm=llm, prompt=chat_prompt)
response = chain.run({"text": user_input})
# Extract and highlight entities in the response
highlighted_response = extract_entities_and_highlight(response)
# Append both user and bot messages to the conversation
conversation.append({'sender': 'User', 'message': user_input})
conversation.append({'sender': 'Bot', 'message': highlighted_response})
return render_template('chat.html', conversation=conversation)
if __name__ == '__main__':
app.run(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment