File size: 2,467 Bytes
5f154d9
 
a2045e1
 
5f154d9
98c78bf
deac740
 
 
 
dc068d2
98c78bf
5f154d9
98c78bf
5f154d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98c78bf
5f154d9
98c78bf
 
 
 
 
 
 
 
a2045e1
98c78bf
5f154d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a2045e1
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
60
61
import os
import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM

# Load the model and tokenizer
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = AutoModelForCausalLM.from_pretrained("openai-community/gpt2-medium")
model.to(device)

tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2-medium", clean_up_tokenization_spaces=True)


# Define the initial prompt for the system
system_prompt = """
You are an AI model designed to provide concise information about big data analytics across various fields without mentioning the question. Respond with a focused, one-line answer that captures the essence of the key risk, benefit, or trend associated with the topic.
input: What do you consider the most significant risk of over-reliance on big data analytics in stock market risk management?
output: Increased market volatility.
input: What is a major benefit of big data analytics in healthcare?
output: Enhanced patient care through personalized treatment.
input: What is a key challenge of big data analytics in retail?
output: Maintaining data privacy and security.
input: What is a primary advantage of big data analytics in manufacturing?
output: Improved production efficiency and predictive maintenance.
input: What is a significant risk associated with big data analytics in education?
output: Potential widening of the achievement gap if data is not used equitably.
"""

def generate(text):
    try:
        # Combine the system prompt with the user's input
        prompt = system_prompt + f"\ninput: {text}\noutput:"

        # Tokenize the input
        inputs = tokenizer(prompt, return_tensors="pt")

        # Generate the response
        outputs = model.generate(inputs["input_ids"], max_length=256)

        # Convert the output to text
        response_text = tokenizer.decode(outputs[0], skip_special_tokens=True).split("output:")[-1].strip()

        return response_text if response_text else "No valid response generated."
    
    except Exception as e:
        return str(e)

iface = gr.Interface(
    fn=generate,
    inputs=gr.Textbox(lines=2, placeholder="Enter text here..."),
    outputs="text",
    title="Big Data Analytics Assistant",
    description="Provides concise information about big data analytics across various fields.",
    live=False
)

def launch_custom_interface():
    iface.launch()

if __name__ == "__main__":
    launch_custom_interface()