File size: 1,604 Bytes
53c8576
 
 
 
 
 
 
 
 
 
 
 
 
299efe1
53c8576
299efe1
 
 
53c8576
 
 
299efe1
53c8576
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
299efe1
 
 
 
 
 
 
 
 
a16d134
 
299efe1
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import gradio as gr
import openai
import os
import json

# 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 = {}

def Get_answer(api_key, prompt_key, question):
    try:
        # Setting the API key for OpenAI
        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}"

# Creating Gradio Interface
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()