ask-theologian / app.py
k2s0's picture
Update app.py
7621c8a
import os
import json
import openai
import gradio as gr
import firebase_admin
from firebase_admin import credentials, firestore
# Get the service account key from the environment variable
service_account_key = os.environ["firebasekey"]
# Parse the service account key into a dictionary
service_account_info = json.loads(service_account_key)
# Create a Certificate object from the service account info
cred = credentials.Certificate(service_account_info)
# Initialize the Firebase Admin SDK
firebase_admin.initialize_app(cred)
# # Create a reference to the Firestore database
db = firestore.client()
openai.api_key = os.environ.get("openai_api_key")
def store_message(user_input, completion):
new_completion = db.collection('askTheologianCompletions').document()
new_completion.set({
'user_input': user_input,
'completion': completion,
'created_time': firestore.SERVER_TIMESTAMP,
'model': 'text-davinci-003',
'temperature': 0.7,
'title': 'Ask Theologian'
})
def greet(input):
myInput = input
myPrompt = f"Religious Tutor: I am a theology professor and religion studies tutor \n You: What is the religious text of the christians? Religious Tutor: The religious text of Christians is the Bible. The Bible is a collection of sacred texts or scriptures that is widely considered to be the word of God by Christians. It is divided into two main parts: the Old Testament, which contains the texts of the Hebrew Bible, and the New Testament, which contains the texts of the Christian faith. \n The Old Testament is made up of 39 books and is considered to be the foundational text of Judaism. It contains the stories, laws, and teachings of the ancient Israelites, as well as prophecies about the coming of the Messiah. \n The New Testament is made up of 27 books and is considered to be the primary source of teachings about Jesus Christ and the Christian faith. It includes the four Gospels (Matthew, Mark, Luke, and John), which contain the accounts of Jesus' life, teachings, and miracles, as well as the Acts of the Apostles, which describes the early spread of Christianity, and the letters (Epistles) written by the apostle Paul and other early Christian leaders. \n\n The Bible is central to the beliefs and practices of Christians and is considered to be the ultimate authority on matters of faith and morals. It is widely read and studied by believers, and is used as a guide for personal and spiritual growth.. \n\n You: {myInput} cite specific text references in all answers"
response = openai.Completion.create(
model="text-davinci-003",
prompt=myPrompt,
temperature=0.7,
max_tokens=3000,
top_p=1.0,
frequency_penalty=0.0,
presence_penalty=0.0
)
raw_response = response['choices'][0]['text']
split_response = raw_response.split('r:')
trimmed_response = split_response[1]
print(trimmed_response)
store_message(myInput, trimmed_response)
return trimmed_response
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
demo.launch()