File size: 3,162 Bytes
6f61b27
 
7fcd6de
 
6f61b27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from transformers import pipeline, AutoModelForSeq2SeqLM, AutoTokenizer
import torch
import gradio as gr

class SunChatbot:
    def __init__(self, model_name="facebook/blenderbot-400M-distill"):
        self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        self.tokenizer = AutoTokenizer.from_pretrained(model_name)
        self.model = AutoModelForSeq2SeqLM.from_pretrained(model_name).to(self.device)
        self.nlp_pipeline = pipeline("text2text-generation", model=self.model, tokenizer=self.tokenizer, device=0 if torch.cuda.is_available() else -1)

    def shine_light_on_errors(self, user_input):
        """Identifies potential errors in input and provides suggestions."""
        correction_prompt = f"Correct any mistakes in the following text: {user_input}"
        return self.nlp_pipeline(correction_prompt, max_length=100)[0]['generated_text']

    def analyze_patterns(self, user_input):
        """Detects patterns and generates insightful analysis."""
        pattern_prompt = f"Analyze the patterns in the following data: {user_input}"
        return self.nlp_pipeline(pattern_prompt, max_length=150)[0]['generated_text']

    def provide_solar_insights(self, user_query):
        """Offers futuristic techniques and correlations in solar technologies."""
        solar_prompt = f"Provide innovative insights on solar technologies: {user_query}"
        return self.nlp_pipeline(solar_prompt, max_length=200)[0]['generated_text']

    def inspire_creativity(self, user_prompt):
        """Provides dynamic brainstorming assistance."""
        creativity_prompt = f"Give me a creative idea related to: {user_prompt}"
        return self.nlp_pipeline(creativity_prompt, max_length=150)[0]['generated_text']

    def handle_tasks_seamlessly(self, task_list):
        """Manages multiple tasks efficiently."""
        task_prompt = f"Manage these tasks efficiently: {task_list}"
        return self.nlp_pipeline(task_prompt, max_length=200)[0]['generated_text']

    def quick_or_detailed_response(self, user_query, detail_level="quick"):
        """Provides concise or detailed responses based on the user's preference."""
        if detail_level == "quick":
            prompt = f"Provide a concise answer to: {user_query}"
        else:
            prompt = f"Provide a detailed analysis of: {user_query}"
        return self.nlp_pipeline(prompt, max_length=250)[0]['generated_text']

    def reframe_negative_thoughts(self, user_input):
        """Reframes negative topics in a constructive light."""
        positive_prompt = f"Reframe the following in a positive way: {user_input}"
        return self.nlp_pipeline(positive_prompt, max_length=150)[0]['generated_text']

def chatbot_interface(user_input, detail_level):
    sun_bot = SunChatbot()
    return sun_bot.quick_or_detailed_response(user_input, detail_level)

gr.Interface(
    fn=chatbot_interface,
    inputs=["text", gr.Radio(["quick", "detailed"], label="Response Detail Level")],
    outputs="text",
    title="Sun Chatbot",
    description="A chatbot that energizes conversations and provides insightful responses inspired by the Sun."
).launch()