Hamada-Fathy commited on
Commit
b0afd07
1 Parent(s): 65d8448

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. README.md +32 -6
  2. app.log +2 -0
  3. openai_intella.py +91 -0
README.md CHANGED
@@ -1,12 +1,38 @@
1
  ---
2
  title: FatwaAi
3
- emoji: 🌍
4
- colorFrom: yellow
5
- colorTo: gray
6
  sdk: gradio
7
  sdk_version: 4.16.0
8
- app_file: app.py
9
- pinned: false
10
  ---
 
 
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  title: FatwaAi
3
+ app_file: openai_intella.py
 
 
4
  sdk: gradio
5
  sdk_version: 4.16.0
 
 
6
  ---
7
+ # Fatwa_UAE
8
+ # OpenAI Question Answering Interface
9
 
10
+ This application utilizes OpenAI's GPT-3.5 model to provide answers to queries based on the content from the [Fatwa UAE website](https://fatwauae.gov.ae/ar/services/fatwa-archive). It features a user-friendly and accessible interface created using Gradio.
11
+
12
+ This application accepting text and audio records from the user in arabic language
13
+
14
+ ## Installation
15
+
16
+ To use this application, certain Python packages are required. Execute the following command in your terminal to install them:
17
+
18
+ ``` bash
19
+ pip install openai gradio openai-whisper
20
+
21
+ ```
22
+
23
+ Ensure Python is installed on your system. This application supports Python version 3.8 or higher.
24
+
25
+ ## Configurations
26
+
27
+ An API key from OpenAI is necessary to access the GPT-3.5 model. Replace the placeholder in the script with your personal API key.
28
+
29
+ ## Running the Application
30
+
31
+ Once you've installed the necessary packages and set up your API key, you can launch the application. Run the Python script either from the command line or via an IDE of your choosing using the following command:
32
+ ``` bash
33
+ python openai_gradio.py
34
+ ```
35
+ Make sure to substitute 'YOUR_API_KEY' in the script with your actual OpenAI API key. Additionally, if the script mentions a logo image file (like 'path_to_your_logo.png'), replace this with the actual path to your logo image.
36
+
37
+ ## Usage
38
+ When the script is operational, a web interface will open. This interface enables users to pose questions and receive answers. These answers are generated by the OpenAI GPT-3.5 model, drawing on the content outlined in the script.
app.log ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ 2024-02-06 14:13:47,449 - DEBUG - Using selector: EpollSelector
2
+ 2024-02-06 14:13:47,572 - DEBUG - Launching Gradio interface...
openai_intella.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from openai import OpenAI
2
+ import gradio as gr
3
+ import logging
4
+ import requests
5
+ import os
6
+ import shutil
7
+ import time
8
+ # Setting up basic logging
9
+ logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s', filename='app.log', filemode='w' )
10
+
11
+ # Configure OpenAI with your API key
12
+ client = OpenAI(api_key='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')
13
+
14
+ def transcribe_with_intella():
15
+ logging.debug(f"Transcribing with Intella")
16
+ # Intella-Voice API endpoint for adding requests
17
+ add_request_url = "https://api.intella-voice.com/api/accounts/84752622-5158-40ef-bab6-d6928892636b/Requests"
18
+ headers = {'x-ApiToken': '78On8sGTQUOaEuY3jxOx/A=='}
19
+
20
+ # Prepare payload and file data
21
+ payload = {'fileName': 'File Name', 'timestamp': '2'}
22
+ files = [('file', ('saved_audio.mp3', open('/home/ubuntu/cntxt/Fatwa_UAE/saved_audio.mp3', 'rb'), 'audio/wav'))]
23
+
24
+ # Send POST request to add the audio file
25
+ add_response = requests.request("POST", add_request_url, headers=headers, data=payload, files=files)
26
+ print('audio text', add_response.text)
27
+ request_id = add_response.json().get('id')
28
+ print('audio text', add_response.text)
29
+ time.sleep(20)
30
+ # Intella-Voice API endpoint to get the transcription
31
+ get_transcription_url = f"https://api.intella-voice.com/api/accounts/84752622-5158-40ef-bab6-d6928892636b/requests/{request_id}"
32
+
33
+ # Send GET request to retrieve the transcription
34
+ get_response = requests.request("GET", get_transcription_url, headers=headers)
35
+ transcription = get_response.json().get('transcriptContent')
36
+ print('transcription', transcription)
37
+ return transcription
38
+
39
+ def answer_question(question):
40
+ logging.debug(f"Answering question: {question}")
41
+ response = client.chat.completions.create(
42
+ model="gpt-3.5-turbo",
43
+ messages=[
44
+ {"role": "system", "content": "You are a helpful assistant."},
45
+ {"role": "user", "content": f"Answer the following question based on the content of https://fatwauae.gov.ae/en/services/fatwa-archive :\n\n{question}"}
46
+ ]
47
+ )
48
+ return response.choices[0].message.content
49
+
50
+ def process_input(question, audio):
51
+ logging.debug(f"Received question: {question}")
52
+ logging.debug(f"Received audio: {audio}")
53
+
54
+ if not question and not audio:
55
+ logging.error("No question or audio provided.")
56
+ return "", "No question provided."
57
+
58
+ if question.strip():
59
+ answer = answer_question(question)
60
+ return "", answer
61
+
62
+ if audio:
63
+ audio_path = "saved_audio.mp3"
64
+ shutil.copyfile(audio, audio_path)
65
+ print('audio saved')
66
+ transcribed_text = transcribe_with_intella()
67
+ answer = answer_question(transcribed_text)
68
+ return transcribed_text, answer
69
+
70
+ # Custom CSS to style the interface
71
+ css = """
72
+ body { font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; }
73
+ .gradio_app { max-width: 800px; margin: auto; background: #f4f4f4; padding: 20px; border-radius: 10px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); }
74
+ .gradio_header { display: flex; align-items: center; justify-content: center; margin-bottom: 20px; }
75
+ .gradio_input_text textarea { height: 100px; }
76
+ .gradio_output_text { white-space: pre-line; }
77
+ .gradio_button { background-color: #106BA3; color: white; }
78
+ """
79
+
80
+ iface = gr.Interface(
81
+ fn=process_input,
82
+ inputs=[gr.Textbox(lines=2, placeholder="Enter your question or use the audio button to record or upload it..."), gr.Audio(type="filepath", label="Or record/upload your question")],
83
+ outputs=[gr.Textbox(label="Transcribed Question"), gr.Textbox(label="Answer")],
84
+ css=css,
85
+ title="Question Answering Interface",
86
+ description="This interface answers your questions based on the content of the Fatwa UAE website in English. Use the text box or record/upload your audio question.",
87
+ allow_flagging=False
88
+ )
89
+
90
+ logging.debug("Launching Gradio interface...")
91
+ iface.launch(share=True, debug = True)