Karlgorithm commited on
Commit
feb85b6
·
verified ·
1 Parent(s): a80221c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -105
app.py CHANGED
@@ -1,41 +1,30 @@
 
1
  import gradio as gr
2
- from transformers import AutoModelForCausalLM, AutoTokenizer
3
- import torch
4
 
5
- # Load model and tokenizer
6
- model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
7
- tokenizer = AutoTokenizer.from_pretrained(model_name)
8
- model = AutoModelForCausalLM.from_pretrained(model_name)
9
 
10
  # Store the last prompt
11
  last_prompt = ""
12
 
13
  def generate(prompt):
14
- """Generation function using local model"""
15
  global last_prompt
16
  last_prompt = prompt
17
-
18
  try:
19
- # Format prompt with chat template
20
- messages = [{"role": "user", "content": prompt}]
21
- input_ids = tokenizer.apply_chat_template(
22
- messages,
23
- return_tensors="pt"
24
- ).to(model.device)
25
-
26
- # Generate response
27
- outputs = model.generate(
28
- input_ids,
29
- max_new_tokens=256,
30
- do_sample=True,
31
- temperature=0.7,
32
- top_p=0.9
33
  )
34
-
35
- # Decode and return the response (skip the prompt)
36
- response = outputs[0][input_ids.shape[-1]:]
37
- return tokenizer.decode(response, skip_special_tokens=True)
38
-
39
  except Exception as e:
40
  return f"Error: {str(e)}"
41
 
@@ -45,51 +34,33 @@ def regenerate():
45
  return generate(last_prompt)
46
  return "No previous prompt to regenerate"
47
 
48
- # Create Gradio interface
49
- with gr.Blocks(title="Karlson Achegeba GPT", theme=gr.themes.Soft(primary_hue="blue")) as app:
50
  with gr.Column(elem_classes=["center-container"]):
51
- # Header Section
52
- gr.Markdown(
53
- """<div style='text-align: center;'>
54
- <h1 style='color: #2563eb; margin-bottom: 0;'>Karlson Achegeba GPT</h1>
55
- <p style='color: #64748b;'>Powered by TinyLlama</p>
56
- </div>"""
57
- )
58
-
59
  # Chat Interface
60
- with gr.Group(elem_classes=["chat-container"]):
61
- # Input Section
62
- with gr.Column():
63
- prompt = gr.Textbox(
64
- placeholder="Type your message here...",
65
- lines=5,
66
- label="Your Message",
67
- elem_classes=["input-box"]
68
- )
69
-
70
- # Action Buttons
71
- with gr.Row():
72
- submit = gr.Button(
73
- "Generate Response",
74
- variant="primary",
75
- elem_classes=["action-button"]
76
- )
77
- regenerate_btn = gr.Button(
78
- "Regenerate",
79
- variant="secondary",
80
- elem_classes=["action-button"]
81
- )
82
- clear_btn = gr.Button(
83
- "Clear",
84
- variant="secondary",
85
- elem_classes=["action-button"]
86
- )
87
-
88
- # Output Section
89
  output = gr.Textbox(
90
- label="AI Response",
91
  interactive=False,
92
- lines=10,
93
  elem_classes=["output-box"]
94
  )
95
 
@@ -98,49 +69,21 @@ with gr.Blocks(title="Karlson Achegeba GPT", theme=gr.themes.Soft(primary_hue="b
98
  regenerate_btn.click(fn=regenerate, outputs=output)
99
  clear_btn.click(fn=lambda: ("", ""), outputs=[prompt, output])
100
 
101
- # Custom CSS (same as before)
102
  app.css = """
103
- .center-container {
104
- max-width: 800px;
105
- margin: auto;
106
- padding: 20px;
107
- }
108
- .chat-container {
109
- display: flex;
110
- flex-direction: column;
111
- gap: 20px;
112
- }
113
- .input-box, .output-box {
114
- border-radius: 8px;
115
  border: 1px solid #e2e8f0;
116
  padding: 12px;
117
  }
118
- .input-box textarea, .output-box textarea {
119
- font-size: 16px !important;
120
- }
121
- .action-button {
122
- border-radius: 8px !important;
123
- padding: 8px 16px !important;
124
- min-width: 120px;
125
- }
126
- .action-button:not(.secondary) {
127
- background: #2563eb !important;
128
- color: white !important;
129
- }
130
- .action-button.secondary {
131
- border: 1px solid #2563eb !important;
132
- color: #2563eb !important;
133
- }
134
- .group {
135
- border: 1px solid #e2e8f0;
136
- border-radius: 12px;
137
- padding: 20px;
138
- background: white;
139
  box-shadow: 0 2px 8px rgba(0,0,0,0.05);
140
  }
141
- .row {
142
- gap: 10px;
143
- }
144
  """
145
 
146
  app.launch()
 
1
+ import requests
2
  import gradio as gr
 
 
3
 
4
+ # Configuration (Update this with your ngrok URL)
5
+ API_URL = "https://5b9f-154-161-168-18.ngrok-free.app/v1/chat/completions"
6
+ HEADERS = {"Content-Type": "application/json"}
 
7
 
8
  # Store the last prompt
9
  last_prompt = ""
10
 
11
  def generate(prompt):
12
+ """Generation function with prompt storage"""
13
  global last_prompt
14
  last_prompt = prompt
 
15
  try:
16
+ response = requests.post(
17
+ API_URL,
18
+ headers=HEADERS,
19
+ json={
20
+ "model": "TinyLlama-1.1B-Chat-v1.0",
21
+ "messages": [{"role": "user", "content": prompt}],
22
+ "max_tokens": 256,
23
+ "temperature": 0.7
24
+ },
25
+ timeout=30
 
 
 
 
26
  )
27
+ return response.json()['choices'][0]['message']['content']
 
 
 
 
28
  except Exception as e:
29
  return f"Error: {str(e)}"
30
 
 
34
  return generate(last_prompt)
35
  return "No previous prompt to regenerate"
36
 
37
+ # Gradio UI
38
+ with gr.Blocks(title="Karlson's TinyLlama Chat", theme=gr.themes.Soft(primary_hue="blue")) as app:
39
  with gr.Column(elem_classes=["center-container"]):
40
+ # Header
41
+ gr.Markdown("""<div style='text-align: center'>
42
+ <h1 style='color: #2563eb'>Karlson's TinyLlama Chat</h1>
43
+ <p>Local model served via ngrok</p>
44
+ </div>""")
45
+
 
 
46
  # Chat Interface
47
+ with gr.Group():
48
+ prompt = gr.Textbox(
49
+ placeholder="Type your message...",
50
+ lines=5,
51
+ label="Your Message",
52
+ elem_classes=["input-box"]
53
+ )
54
+
55
+ with gr.Row():
56
+ submit = gr.Button("Generate", variant="primary")
57
+ regenerate_btn = gr.Button("Regenerate", variant="secondary")
58
+ clear_btn = gr.Button("Clear All", variant="secondary")
59
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  output = gr.Textbox(
61
+ label="AI Response",
62
  interactive=False,
63
+ lines=8,
64
  elem_classes=["output-box"]
65
  )
66
 
 
69
  regenerate_btn.click(fn=regenerate, outputs=output)
70
  clear_btn.click(fn=lambda: ("", ""), outputs=[prompt, output])
71
 
72
+ # CSS Styling
73
  app.css = """
74
+ .center-container { max-width: 800px; margin: auto; padding: 20px; }
75
+ .input-box, .output-box {
76
+ border-radius: 8px;
 
 
 
 
 
 
 
 
 
77
  border: 1px solid #e2e8f0;
78
  padding: 12px;
79
  }
80
+ .action-button { min-width: 120px; }
81
+ .group {
82
+ background: white;
83
+ border-radius: 12px;
84
+ padding: 20px;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  box-shadow: 0 2px 8px rgba(0,0,0,0.05);
86
  }
 
 
 
87
  """
88
 
89
  app.launch()