import gradio as gr import openai import os import json from getpass import getpass # Importing the getpass library # Ask user to input API Key and keep it hidden API_Key = getpass("Please enter your OpenAI API Key: ") # Load the Prompt dictionary from a JSON file try: with open('prompts.json', 'r') as f: Prompt = json.load(f) except FileNotFoundError: print("prompts.json file not found. Using an empty dictionary instead.") Prompt = {} # Set API Key openai.api_key = API_Key def Get_answer(prompt_key, question): try: messages = [ { "role": "system", "content": Prompt[prompt_key] }, { "role": "user", "content": f"{question}" } ] response = openai.ChatCompletion.create( model="gpt-4", messages=messages, temperature=0, max_tokens=4904, top_p=1, frequency_penalty=0, presence_penalty=0 ) return response['choices'][0]['message']['content'] except Exception as e: return f"An error occurred: {e}" # Creating Gradio Interface interface = gr.Interface(fn=Get_answer, inputs=[ gr.inputs.Dropdown(choices=list(Prompt.keys()), label="Select a Prompt"), gr.inputs.Textbox(lines=5, placeholder="Type your question here...") ], outputs="text", live=False, title="Muslim Imam Guidance", description="Ask any question and get guidance based on Quran, Hadith, and Sunnah.") interface.launch()