AI-Clone / app.py
Karthik Raja
Update app.py
c440694 verified
raw
history blame
1.66 kB
import gradio as gr
import openai
import os
from openai import OpenAI
client = OpenAI()
# Load your OpenAI API key from the environment variable
openai.api_key = os.getenv("OPENAI_API_KEY")
# Read the static CV file
def load_cv():
with open("templated_CV.txt", 'r') as file:
return file.read()
# Extract information from the CV
cv_text = load_cv()
# Initialize a history list to keep track of the conversation
history = []
def chat_with_ai(user_input):
global history
# Append user message to history
history.append({"role": "user", "content": user_input})
# Limit history to the last 20 messages
if len(history) > 20:
history = history[-20:]
# Prepare the messages for the API call, including the CV text
messages = [
{"role": "system", "content": "You are Karthik Raja, and the following details are your academic and research achievements and industry experiences."},
{"role": "system", "content": cv_text}
] + history
# Make the API call
completion = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=messages
)
assistant_message = completion.choices[0].message
# Append assistant message to history
history.append(assistant_message)
return assistant_message['content']
def main(user_input):
response = chat_with_ai(user_input)
return response
iface = gr.Interface(
fn=main,
inputs=gr.Textbox(label="Ask a question, that you would like to ask Karthik"),
outputs="text",
title="AI Clone",
description="Interact with an AI clone for recruiting or for fun :)"
)
iface.launch()