File size: 4,540 Bytes
4ade08c
d5c72cc
 
ab546a4
 
b03e00d
d5c72cc
 
 
 
 
 
 
 
 
16a33a8
 
d5c72cc
 
 
 
 
 
 
 
ab546a4
 
 
 
 
 
 
 
 
 
 
ec230fe
ab546a4
 
 
 
 
 
 
ec230fe
ab546a4
d5c72cc
ab546a4
d5c72cc
 
 
 
 
 
 
ab546a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b03e00d
ab546a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b03e00d
ab546a4
 
 
 
 
 
 
 
 
 
 
 
 
 
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import gradio as gr
from llama_cpp import Llama
from huggingface_hub import hf_hub_download
import json
import re

def load_model():
    repo_id = "forestav/gguf_lora_model"
    model_file = "unsloth.F16.gguf"  
    
    local_path = hf_hub_download(
        repo_id=repo_id,
        filename=model_file
    )
    
    print(f"Loading model from: {local_path}")
    
    model = Llama(
        model_path=local_path,
        n_ctx=2048,
        n_threads=8
    )
    
    return model

# Enhanced generation with multiple modes
def generate_response(message, history, mode='chat'):
    # Preprocessing based on mode
    if mode == 'code':
        system_prompt = "You are an expert coding assistant. Provide clean, efficient code solutions."
    elif mode == 'creative':
        system_prompt = "You are a creative writing assistant. Generate imaginative and engaging content."
    elif mode == 'analytical':
        system_prompt = "You are an analytical assistant. Provide deep, structured insights and reasoning."
    else:
        system_prompt = "You are a helpful AI assistant."
    
    # Prepare messages with system context
    messages = [
        {"role": "system", "content": system_prompt},
        *[{"role": "user" if i % 2 == 0 else "assistant", "content": msg} 
          for i, msg in enumerate(sum(history, []))],
        {"role": "user", "content": message}
    ]
    
    # Generate response
    response = model.create_chat_completion(
        messages=messages,
        max_tokens=512,
        temperature=0.7,
        top_p=0.95,
    )
    
    return response['choices'][0]['message']['content']

# Extract structured data from text
def extract_structured_data(text):
    try:
        # Try to extract JSON-like structures
        json_match = re.search(r'\{.*\}', text, re.DOTALL)
        if json_match:
            try:
                return json.loads(json_match.group(0))
            except json.JSONDecodeError:
                pass
        
        # Fall back to custom parsing for key-value pairs
        data = {}
        for line in text.split('\n'):
            if ':' in line:
                key, value = line.split(':', 1)
                data[key.strip()] = value.strip()
        
        return data
    except Exception as e:
        return {"error": str(e)}

# Create Gradio interface with multiple tabs
def create_interface():
    with gr.Blocks() as demo:
        gr.Markdown("# Multi-Mode AI Assistant")
        
        with gr.Tabs():
            # Chat Interface
            with gr.TabItem("Conversational Chat"):
                chat_interface = gr.ChatInterface(
                    fn=lambda message, history: generate_response(message, history, 'chat'),
                    title="Conversational AI",
                    description="General-purpose conversation mode"
                )
            
            # Code Generation Tab
            with gr.TabItem("Code Assistant"):
                code_interface = gr.ChatInterface(
                    fn=lambda message, history: generate_response(message, history, 'code'),
                    title="AI Code Generator",
                    description="Generate code snippets and solve programming challenges"
                )
            
            # Creative Writing Tab
            with gr.TabItem("Creative Writing"):
                creative_interface = gr.ChatInterface(
                    fn=lambda message, history: generate_response(message, history, 'creative'),
                    title="Creative Writing Assistant",
                    description="Generate stories, poems, and creative content"
                )
            
            # Data Extraction Tab
            with gr.TabItem("Data Extractor"):
                with gr.Row():
                    text_input = gr.Textbox(label="Input Text")
                    extract_btn = gr.Button("Extract Structured Data")
                    json_output = gr.JSON(label="Extracted Data")
                
                extract_btn.click(
                    fn=extract_structured_data, 
                    inputs=text_input, 
                    outputs=json_output
                )
    
    return demo

# Load model globally
print("Starting model loading...")
model = load_model()
print("Model loaded successfully!")

# Create and launch the interface
demo = create_interface()
demo.launch(
    server_name="0.0.0.0",  # Necessary for Spaces
    server_port=7860,       # Standard port for Spaces
    share=True            # Don't need share link in Spaces
)