iabualhaol commited on
Commit
53c8576
1 Parent(s): 2dd541b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import openai
3
+ import os
4
+ import json
5
+ from getpass import getpass # Importing the getpass library
6
+
7
+ # Ask user to input API Key and keep it hidden
8
+ API_Key = getpass("Please enter your OpenAI API Key: ")
9
+
10
+ # Load the Prompt dictionary from a JSON file
11
+ try:
12
+ with open('prompts.json', 'r') as f:
13
+ Prompt = json.load(f)
14
+ except FileNotFoundError:
15
+ print("prompts.json file not found. Using an empty dictionary instead.")
16
+ Prompt = {}
17
+
18
+
19
+
20
+ # Set API Key
21
+ openai.api_key = API_Key
22
+ def Get_answer(prompt_key, question):
23
+ try:
24
+ messages = [
25
+ {
26
+ "role": "system",
27
+ "content": Prompt[prompt_key]
28
+ },
29
+ {
30
+ "role": "user",
31
+ "content": f"{question}"
32
+ }
33
+ ]
34
+
35
+ response = openai.ChatCompletion.create(
36
+ model="gpt-4",
37
+ messages=messages,
38
+ temperature=0,
39
+ max_tokens=4904,
40
+ top_p=1,
41
+ frequency_penalty=0,
42
+ presence_penalty=0
43
+ )
44
+
45
+ return response['choices'][0]['message']['content']
46
+ except Exception as e:
47
+ return f"An error occurred: {e}"
48
+
49
+ # Creating Gradio Interface
50
+ interface = gr.Interface(fn=Get_answer,
51
+ inputs=[
52
+ gr.inputs.Dropdown(choices=list(Prompt.keys()), label="Select a Prompt"),
53
+ gr.inputs.Textbox(lines=5, placeholder="Type your question here...")
54
+ ],
55
+ outputs="text",
56
+ live=False,
57
+ title="Muslim Imam Guidance",
58
+ description="Ask any question and get guidance based on Quran, Hadith, and Sunnah.")
59
+ interface.launch()