Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""Untitled1.ipynb
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1Oyv-OC4NLyS4SOfffFLwdI0wqvX7K_q2
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
!pip install transformers PyPDF2 gradio
|
| 11 |
+
|
| 12 |
+
import gradio as gr
|
| 13 |
+
from transformers import pipeline
|
| 14 |
+
from PyPDF2 import PdfReader
|
| 15 |
+
from huggingface_hub import InferenceClient
|
| 16 |
+
from google.colab import userdata
|
| 17 |
+
import requests
|
| 18 |
+
from PIL import Image
|
| 19 |
+
import io
|
| 20 |
+
|
| 21 |
+
pipe = pipeline("text2text-generation", model="asach/simpleT5-resume-summarization")
|
| 22 |
+
|
| 23 |
+
reader = PdfReader("/KennethGuillont.pdf")
|
| 24 |
+
text = ""
|
| 25 |
+
for page in reader.pages:
|
| 26 |
+
text += page.extract_text()
|
| 27 |
+
|
| 28 |
+
summary = pipe(text, max_length=150, min_length=30)[0]['generated_text']
|
| 29 |
+
summary
|
| 30 |
+
|
| 31 |
+
my_key = userdata.get('HF')
|
| 32 |
+
|
| 33 |
+
client = InferenceClient(api_key=my_key)
|
| 34 |
+
|
| 35 |
+
model_name= 'meta-llama/Llama-3.2-3B-Instruct'
|
| 36 |
+
|
| 37 |
+
agent_desc = """
|
| 38 |
+
You are an AI agent helps a user generate a prompt to feed into an AI image
|
| 39 |
+
generation model based on a summary of their resume given to you. The image should depict a rabbit
|
| 40 |
+
within the the career feild related to the summary. encase the image prompt between
|
| 41 |
+
two '---\n' marks, to separate it from the rest of the text.
|
| 42 |
+
"""
|
| 43 |
+
|
| 44 |
+
print(summary)
|
| 45 |
+
|
| 46 |
+
messages = [
|
| 47 |
+
{"role": "user", "content": agent_desc},
|
| 48 |
+
{"role": "user", "content": summary}
|
| 49 |
+
]
|
| 50 |
+
|
| 51 |
+
stream = client.chat.completions.create(
|
| 52 |
+
model=model_name,
|
| 53 |
+
messages=messages,
|
| 54 |
+
max_tokens=700,
|
| 55 |
+
stream=True
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
response_text =""
|
| 59 |
+
|
| 60 |
+
for chunk in stream:
|
| 61 |
+
response_text += chunk.choices[0].delta.content
|
| 62 |
+
|
| 63 |
+
print(response_text)
|
| 64 |
+
|
| 65 |
+
print(response_text.replace('.','.\n'))
|
| 66 |
+
|
| 67 |
+
image_prompt = response_text.split('---\n')[1]
|
| 68 |
+
image_prompt
|
| 69 |
+
|
| 70 |
+
API_URL = "https://api-inference.huggingface.co/models/CompVis/stable-diffusion-v1-4"
|
| 71 |
+
headers = {"Authorization": f"Bearer {my_key}"}
|
| 72 |
+
|
| 73 |
+
def query(payload):
|
| 74 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 75 |
+
return response.content
|
| 76 |
+
|
| 77 |
+
image_bytes = query({
|
| 78 |
+
"inputs": image_prompt,
|
| 79 |
+
})
|
| 80 |
+
# You can access the image with PIL.Image for example
|
| 81 |
+
import io
|
| 82 |
+
from PIL import Image
|
| 83 |
+
image = Image.open(io.BytesIO(image_bytes))
|
| 84 |
+
|
| 85 |
+
image
|