Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import openai
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import os # Importing os to access environment variables
|
| 5 |
+
from datasets import load_dataset
|
| 6 |
+
|
| 7 |
+
# Load the dataset from Hugging Face
|
| 8 |
+
dataset = load_dataset('dindizz/musicacademyarchives')
|
| 9 |
+
|
| 10 |
+
# Access the OpenAI API key from environment variables (Hugging Face secret)
|
| 11 |
+
openai.api_key = os.getenv('OPENAI_API_KEY')
|
| 12 |
+
|
| 13 |
+
def extract_info(query):
|
| 14 |
+
"""
|
| 15 |
+
This function interacts with OpenAI GPT-3.5 Turbo to extract information from the dataset based on the user's query.
|
| 16 |
+
"""
|
| 17 |
+
# Extracting the text content from the dataset to pass as context
|
| 18 |
+
all_souvenirs = []
|
| 19 |
+
for item in dataset['train']:
|
| 20 |
+
souvenir_text = item['text'] # Assuming the column name is 'text' containing the content
|
| 21 |
+
all_souvenirs.append(souvenir_text)
|
| 22 |
+
|
| 23 |
+
# Combine the content into a single string (you can adjust based on the size of the dataset)
|
| 24 |
+
combined_souvenir_text = "\n".join(all_souvenirs)
|
| 25 |
+
|
| 26 |
+
# Prompt OpenAI GPT-3.5 with the user's query and the combined text
|
| 27 |
+
prompt = f"Extract relevant information based on the following query: '{query}' from the Madras Music Academy Souvenir archives: {combined_souvenir_text[:2000]}" # limiting the length for performance
|
| 28 |
+
|
| 29 |
+
response = openai.ChatCompletion.create(
|
| 30 |
+
model="gpt-3.5-turbo", # Updated model
|
| 31 |
+
messages=[
|
| 32 |
+
{"role": "system", "content": "You are an assistant that extracts information from the Madras Music Academy Souvenir dataset and present in a friendly tone ."},
|
| 33 |
+
{"role": "user", "content": prompt}
|
| 34 |
+
],
|
| 35 |
+
max_tokens=300
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# Returning the answer from OpenAI GPT-3.5 Turbo
|
| 39 |
+
answer = response['choices'][0]['message']['content']
|
| 40 |
+
return answer.strip()
|
| 41 |
+
|
| 42 |
+
# Define the Gradio interface
|
| 43 |
+
def gradio_interface(query):
|
| 44 |
+
return extract_info(query)
|
| 45 |
+
|
| 46 |
+
# Launch the Gradio app
|
| 47 |
+
iface = gr.Interface(
|
| 48 |
+
fn=gradio_interface,
|
| 49 |
+
inputs="text",
|
| 50 |
+
outputs="text",
|
| 51 |
+
title="Sabha Scholar - Madras Music Academy AI Explorer",
|
| 52 |
+
description="Ask questions about the Madras Music Academy Souvenirs and extract information using OpenAI GPT-3.5 Turbo."
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
iface.launch()
|