Spaces:
Sleeping
Sleeping
Delete Ahmed.py
Browse files
Ahmed.py
DELETED
|
@@ -1,55 +0,0 @@
|
|
| 1 |
-
from flask import Flask, render_template, request
|
| 2 |
-
from flask_sqlalchemy import SQLAlchemy
|
| 3 |
-
import os
|
| 4 |
-
import gradio as gr
|
| 5 |
-
from langchain.chat_models import ChatOpenAI
|
| 6 |
-
from langchain import LLMChain, PromptTemplate
|
| 7 |
-
from langchain.memory import ConversationBufferMemory
|
| 8 |
-
|
| 9 |
-
app = Flask(__name__)
|
| 10 |
-
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///chat_history.db'
|
| 11 |
-
db = SQLAlchemy(app)
|
| 12 |
-
|
| 13 |
-
class ChatHistory(db.Model):
|
| 14 |
-
id = db.Column(db.Integer, primary_key=True)
|
| 15 |
-
user_message = db.Column(db.String(255))
|
| 16 |
-
chatbot_response = db.Column(db.String(255))
|
| 17 |
-
|
| 18 |
-
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
|
| 19 |
-
|
| 20 |
-
template = """You are a helpful assistant to answer all user queries.
|
| 21 |
-
{chat_history}
|
| 22 |
-
User: {user_message}
|
| 23 |
-
Chatbot:"""
|
| 24 |
-
|
| 25 |
-
prompt = PromptTemplate(
|
| 26 |
-
input_variables=["chat_history", "user_message"], template=template
|
| 27 |
-
)
|
| 28 |
-
|
| 29 |
-
memory = ConversationBufferMemory(memory_key="chat_history")
|
| 30 |
-
|
| 31 |
-
llm_chain = LLMChain(
|
| 32 |
-
llm=ChatOpenAI(temperature='0.5', model_name="gpt-3.5-turbo"),
|
| 33 |
-
prompt=prompt,
|
| 34 |
-
verbose=True,
|
| 35 |
-
memory=memory,
|
| 36 |
-
)
|
| 37 |
-
|
| 38 |
-
def save_to_database(user_message, chatbot_response):
|
| 39 |
-
entry = ChatHistory(user_message=user_message, chatbot_response=chatbot_response)
|
| 40 |
-
db.session.add(entry)
|
| 41 |
-
db.session.commit()
|
| 42 |
-
|
| 43 |
-
def get_text_response(user_message, history):
|
| 44 |
-
response = llm_chain.predict(user_message=user_message)
|
| 45 |
-
save_to_database(user_message, response)
|
| 46 |
-
return response
|
| 47 |
-
|
| 48 |
-
@app.route('/')
|
| 49 |
-
def index():
|
| 50 |
-
chat_history_entries = ChatHistory.query.all()
|
| 51 |
-
return render_template('index.html', chat_history_entries=chat_history_entries)
|
| 52 |
-
|
| 53 |
-
if __name__ == "__main__":
|
| 54 |
-
db.create_all()
|
| 55 |
-
gr.Interface(fn=get_text_response, live=True).launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|