|
import gradio as gr |
|
import openai |
|
import os |
|
import json |
|
|
|
|
|
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 = {} |
|
|
|
def Get_answer(api_key, prompt_key, question): |
|
try: |
|
|
|
openai.api_key = api_key |
|
|
|
messages = [ |
|
{ |
|
"role": "system", |
|
"content": Prompt.get(prompt_key, "Unknown prompt") |
|
}, |
|
{ |
|
"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}" |
|
|
|
|
|
interface = gr.Interface( |
|
fn=Get_answer, |
|
inputs=[ |
|
gr.inputs.Textbox(lines=1, label="Enter your OpenAI API Key", type="password"), |
|
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", |
|
description="Ask any question? And Select you Imam focus: English, Arabic, and Arabic Quranic" |
|
) |
|
|
|
interface.launch() |
|
|