JimmyTsang commited on
Commit
b0a97f0
·
verified ·
1 Parent(s): 3566d93

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -31
app.py CHANGED
@@ -1,38 +1,56 @@
 
1
  import gradio as gr
2
- import transformers
3
- import torch
4
- import os
5
 
6
- # Ensure your Hugging Face token is set
7
- TOKEN = os.getenv('HF_TOKEN')
8
 
9
- model_id = "meta-llama/Meta-Llama-3.1-8B-Instruct"
 
 
 
 
 
 
 
10
 
11
- pipeline = transformers.pipeline(
12
- "text-generation",
13
- model=model_id,
14
- model_kwargs={"torch_dtype": torch.bfloat16},
15
- device_map="cpu",
16
- token=TOKEN
17
- )
18
 
19
- # Define the respond function to generate test cases for a code solution
20
- def respond(code_problem, code_solution, max_tokens):
21
- prompt = f"Generate test cases to test the following code solution:\n\nProblem:\n{code_problem}\n\nSolution:\n{code_solution}\n\nTest Cases:"
22
- outputs = pipeline(prompt, max_new_tokens=max)
23
- return outputs[0]["generated_text"][-1]
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
- # Define Gradio interface
26
- interface = gr.Interface(
27
- fn=respond,
28
- inputs=[
29
- gr.Textbox(lines=10, placeholder="Describe the code problem here...", label="Code Problem"),
30
- gr.Textbox(lines=10, placeholder="Provide the code solution here...", label="Code Solution"),
31
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
32
- ],
33
- outputs=gr.Textbox(lines=20, label="Generated Test Cases"),
34
- title="Code Test Case Generator",
35
- description="Input a code problem and its solution to generate test cases using the Meta-Llama-3.1-8B-Instruct model."
36
- )
37
 
38
- interface.launch()
 
 
1
+ from transformers import GPTNeoForCausalLM, GPT2Tokenizer
2
  import gradio as gr
 
 
 
3
 
4
+ model = GPTNeoForCausalLM.from_pretrained("EleutherAI/gpt-neo-125M")
5
+ tokenizer = GPT2Tokenizer.from_pretrained("EleutherAI/gpt-neo-125M")
6
 
7
+ prompt = """This is a discussion between a person and Hassan Kane, an entrepreneur.
8
+ person: What are you working on?
9
+ Hassan: This new AI community building the future of Africa
10
+ person: Where are you?
11
+ Hassan: In Lagos for a week, then Paris or London.
12
+ person: How's it going?
13
+ Hassan: Not bad.. Just trying to hit EV (escape velocity) with my startup
14
+ person: """
15
 
16
+ def my_split(s, seps):
17
+ res = [s]
18
+ for sep in seps:
19
+ s, res = res, []
20
+ for seq in s:
21
+ res += seq.split(sep)
22
+ return res
23
 
24
+ # input = "Who are you?"
25
+ def chat_base(input):
26
+ p = prompt + input
27
+ input_ids = tokenizer(p, return_tensors="pt").input_ids
28
+ gen_tokens = model.generate(input_ids, do_sample=True, temperature=0.7, max_length=150,)
29
+ gen_text = tokenizer.batch_decode(gen_tokens)[0]
30
+ # print(gen_text)
31
+ result = gen_text[len(p):]
32
+ # print(">", result)
33
+ result = my_split(result, [']', '\n'])[1]
34
+ # print(">>", result)
35
+ if "Hassan: " in result:
36
+ result = result.split("Hassan: ")[-1]
37
+ # print(">>>", result)
38
+ return result
39
+
40
+ import gradio as gr
41
 
42
+ def chat(message):
43
+ history = gr.get_state() or []
44
+ print(history)
45
+ response = chat_base(message)
46
+ history.append((message, response))
47
+ gr.set_state(history)
48
+ html = "<div class='chatbot'>"
49
+ for user_msg, resp_msg in history:
50
+ html += f"<div class='user_msg'>{user_msg}</div>"
51
+ html += f"<div class='resp_msg'>{resp_msg}</div>"
52
+ html += "</div>"
53
+ return response
54
 
55
+ iface = gr.Interface(chat_base, gr.inputs.Textbox(label="Ask Hassan a Question"), "text", allow_screenshot=False, allow_flagging=False)
56
+ iface.launch()