Spaces:
Paused
Paused
| import subprocess | |
| import sys | |
| def install(package): | |
| subprocess.check_call([sys.executable, "-m", "pip", "install", package]) | |
| install("openai==0.28") | |
| import json | |
| from pathlib import Path | |
| import gradio as gr | |
| import os | |
| import shutil | |
| import json | |
| import openai | |
| api_key = os.getenv("api_key") | |
| # Load the OpenAI API key | |
| openai.api_key = api_key | |
| project_data = {} | |
| def ask_chatbot(query, chat_history, project_data): | |
| # Convert project_data to a string format (optional: simplify or summarize if needed) | |
| project_data_str = json.dumps(project_data, indent=2) | |
| if len(chat_history) >= 10: | |
| chat_history.pop(0) | |
| query_with_history = "" | |
| for question, answer in chat_history: | |
| query_with_history += f"\nUser: {question}\nAssistant: {answer}" | |
| query_with_history += f"\nUser: {query}" | |
| # Define the messages | |
| messages = [ | |
| {"role": "system", "content": "You are a virtual project management assistant. \ | |
| Analyzing the given project information for a website redesign project, you have to answer project managers' questions.:"}, | |
| {"role": "system", "content": project_data_str}, | |
| {"role": "user", "content": query_with_history} | |
| ] | |
| # Call the OpenAI API | |
| response = openai.ChatCompletion.create( | |
| model="gpt-3.5-turbo", | |
| messages=messages, | |
| max_tokens=512 | |
| ) | |
| # Extract the response text | |
| answer = response.choices[0].message['content'].strip() | |
| return answer | |
| def generate_response(message, history): | |
| # project_data = {} | |
| # if os.path.exists("/content/project_data.json"): | |
| # with open("/content/project_data.json", 'r') as f: | |
| # project_data = json.load(f) | |
| return ask_chatbot(message, history, project_data) | |
| def upload_file(data_file): | |
| with open(data_file.name, "r") as f: | |
| data = json.load(f) | |
| gr.Info("Project file Uploaded. You can now query the document") | |
| # with open("/content/project_data.json", "w") as f: | |
| # json.dump(data, f) | |
| global project_data | |
| project_data = data | |
| with gr.Blocks() as demo: | |
| gr.ChatInterface( | |
| generate_response, | |
| chatbot=gr.Chatbot(height=500), | |
| title="ProManage", | |
| description="Virtual Project Management Assistant", | |
| theme="soft", | |
| undo_btn="Delete Previous", | |
| clear_btn="Clear", | |
| ) | |
| with gr.Column(): | |
| u = gr.UploadButton("Upload a file", file_count="single") | |
| u.upload(upload_file, u) | |
| demo.launch() | |