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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -20
app.py CHANGED
@@ -1,40 +1,51 @@
1
- import requests
2
  import gradio as gr
 
 
3
 
4
- # Minimal Configuration
5
- API_URL = "http://127.0.0.1:8080/v1/chat/completions"
 
 
6
 
7
  # Store the last prompt
8
  last_prompt = ""
9
 
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
- json={
19
- "model": "TinyLlama-1.1B-Chat-v1.0",
20
- "messages": [{"role": "user", "content": prompt}],
21
- "max_tokens": 256
22
- },
23
- timeout=100
 
 
 
 
 
 
24
  )
25
- return response.json()['choices'][0]['message']['content']
 
 
 
 
26
  except Exception as e:
27
  return f"Error: {str(e)}"
28
 
29
-
30
  def regenerate():
31
  """Regenerate response using last prompt"""
32
  if last_prompt:
33
  return generate(last_prompt)
34
  return "No previous prompt to regenerate"
35
 
36
-
37
- # Styled UI with improved organization
38
  with gr.Blocks(title="Karlson Achegeba GPT", theme=gr.themes.Soft(primary_hue="blue")) as app:
39
  with gr.Column(elem_classes=["center-container"]):
40
  # Header Section
@@ -85,9 +96,9 @@ with gr.Blocks(title="Karlson Achegeba GPT", theme=gr.themes.Soft(primary_hue="b
85
  # Event Handlers
86
  submit.click(fn=generate, inputs=prompt, outputs=output)
87
  regenerate_btn.click(fn=regenerate, outputs=output)
88
- clear_btn.click(fn=lambda: ("", ""), outputs=[prompt, output]) # Clears both input and output
89
 
90
- # Custom CSS
91
  app.css = """
92
  .center-container {
93
  max-width: 800px;
@@ -131,4 +142,5 @@ with gr.Blocks(title="Karlson Achegeba GPT", theme=gr.themes.Soft(primary_hue="b
131
  gap: 10px;
132
  }
133
  """
134
- app.launch(server_port=7860, share=True)
 
 
 
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
 
 
42
  def regenerate():
43
  """Regenerate response using last prompt"""
44
  if last_prompt:
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
 
96
  # Event Handlers
97
  submit.click(fn=generate, inputs=prompt, outputs=output)
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;
 
142
  gap: 10px;
143
  }
144
  """
145
+
146
+ app.launch()